Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to track SelectedItem of DataGrid inside RowDetailsTemplate of another Datagrid
    text
    copied!<p>I ran into this problem earlier today and am unable to find a solution. The <code>SelectedItem</code> of a <code>DataGrid</code> that's inside the <code>RowDetailsTemplate</code> of another <code>DataGrid</code> is not being set when I select a row inside the <code>DataGrid</code> that's inside the <code>RowDetailsTemplate</code>. (Difficult to explain clearly.)</p> <p>The bindings are all working properly for the lists. The MainViewModel contains an <code>ObservableCollection</code> of <code>MyItem</code> objects and that's what the outer <code>DataGrid</code> binds to. </p> <p>The <code>MyItem</code> object contains an <code>ObservableCollection</code> of <code>MyItem2</code> objects and they get bound correctly to the inner <code>DataGrid</code>. </p> <p>The <code>MyItem</code> object also has a property called <code>SelectedItem2</code> which is supposed to be bound to the <code>SelectedItem</code> of the inner <code>DataGrid</code> but never gets set.</p> <p>BTW: I'm using VS2012 and .Net 4.5.</p> <p>Example:</p> <p><code>MainWindow.xaml</code></p> <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfApplication1="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.Resources&gt; &lt;wpfApplication1:MainWindowViewModel x:Key="MainVm"/&gt; &lt;/Window.Resources&gt; &lt;Grid DataContext="{StaticResource MainVm}"&gt; &lt;DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=MyItem1s}"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Name" Width="*" Binding="{Binding Path=Name}"/&gt; &lt;/DataGrid.Columns&gt; &lt;DataGrid.RowDetailsTemplate&gt; &lt;DataTemplate&gt; &lt;DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Item2s}" SelectedItem="{Binding Path=SelectedItem2}"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Item 2 Name" Width="130" Binding="{Binding Path=Name}"/&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;/DataTemplate&gt; &lt;/DataGrid.RowDetailsTemplate&gt; &lt;/DataGrid&gt; &lt;/Grid&gt; </code></pre> <p></p> <p><code>MainWindowViewModel</code></p> <pre><code> public class MainWindowViewModel : ViewModelBase { public MainWindowViewModel() { for (int i = 0; i &lt; 10; i++) { var item1 = new MyItem1(); item1.Name = i.ToString(); for (int j = 11; j &lt; 20; j++) item1.Item2s.Add(new MyItem2() { Name = j.ToString() }); MyItem1s.Add(item1); } } private ObservableCollection&lt;MyItem1&gt; _myItem1S = new ObservableCollection&lt;MyItem1&gt;(); public ObservableCollection&lt;MyItem1&gt; MyItem1s { get { return _myItem1S; } set { _myItem1S = value; } } } </code></pre> <p><code>MyItems ViewModels</code></p> <pre><code> public class MyItem1 : ViewModelBase { private string _name; public string Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } } private ObservableCollection&lt;MyItem2&gt; _item2S = new ObservableCollection&lt;MyItem2&gt;(); public ObservableCollection&lt;MyItem2&gt; Item2s { get { return _item2S; } set { _item2S = value; OnPropertyChanged("Item2s"); } } private MyItem2 _selectedItem2; public MyItem2 SelectedItem2 { get { return _selectedItem2; } set { _selectedItem2 = value; OnPropertyChanged("SelectedItem2"); } } } public class MyItem2 : ViewModelBase { private string _name; public string Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } } } </code></pre> <p><code>ViewModelBase</code></p> <pre><code>public class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string property) { if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(property)); } } </code></pre> <p>Any Help would be appreciated. </p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload