Skip to content

Instantly share code, notes, and snippets.

@McTopaz
Created March 15, 2018 12:11
Show Gist options
  • Save McTopaz/80cd81cc6778f0c17a0837601ec93acf to your computer and use it in GitHub Desktop.
Save McTopaz/80cd81cc6778f0c17a0837601ec93acf to your computer and use it in GitHub Desktop.
Bind the selected string in a TextBlock inside a ListView inside a cell of a DataGrid to a ViewModel
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
<PropertyChanged />
</Weavers>
<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding Rows}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<!-- ID -->
<DataGridTextColumn Header="ID" Binding="{Binding ID}" />
<!-- List with names -->
<DataGridTemplateColumn Header="Names">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Names}">
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel LastChildFill="True">
<Button Content="Change" DockPanel.Dock="Right" Command="{Binding Path=DataContext.ChangeNameButton, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
<TextBlock Text="{Binding}" Margin="0,0,3,0" DockPanel.Dock="Left"/>
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Some data to work with.
var model1 = new Model();
model1.ID = 5;
model1.Names.Add("Name1");
model1.Names.Add("Name2");
var model2 = new Model();
model2.ID = 2;
model2.Names.Add("NameA");
model2.Names.Add("NameB");
model2.Names.Add("NameC");
// Setup ViewModel and View.
var vm = this.DataContext as ViewModel;
vm.Rows.Add(model1);
vm.Rows.Add(model2);
vm.ChangeNameButton.Callback = ChangeName;
}
public void ChangeName()
{
var vm = this.DataContext as ViewModel;
Console.WriteLine($"Current selected name is: {vm.SelectedName}");
vm.SelectedName = "Some other name";
}
}
public class Model
{
public int ID { get; set; }
public System.Collections.ObjectModel.ObservableCollection<string> Names { get; set; }
public Model()
{
Names = new System.Collections.ObjectModel.ObservableCollection<string>();
}
}
public class Btn : ICommand
{
public event EventHandler CanExecuteChanged;
public Action Callback { get; set; }
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
Callback();
}
}
[PropertyChanged.AddINotifyPropertyChangedInterface]
public class ViewModel
{
public System.Collections.ObjectModel.ObservableCollection<Model> Rows { get; set; }
public Btn ChangeNameButton { get; set; }
public string SelectedName { get; set; }
public ViewModel()
{
Rows = new System.Collections.ObjectModel.ObservableCollection<Model>();
ChangeNameButton = new Btn();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment