I am developing a wpf application based on MVVM. All I am doing is trying to bind Nested DataGrids with my DataTable. I have class CustomTable
public class CustomTable : INotifyPropertyChanged
{
public List<DataTable> Main { get; set; }
public CustomTable Child { get; set; }
public DataRowView _selectedItem;
public DataRowView SelectedItem
{
get
{
return _selectedItem;
}
set
{
_selectedItem = value;
Child = new CustomTable();
OnPropertyChanged("SelectedItem");
}
}
public CustomTable()
{
Main = new List<DataTable>();
Main.Add(someRandomTable());
}
private DataTable someRandomTable()
{
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string caller)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(caller));
}
}
}
Custom Table consist of List of DataTables -> Main, SelectedItem, and Child of CustomTable type. I am implementing INotifyPropertyChanged to this class.
So I have ItemsControl whose DataContext is this class and ItemSource binds to Main. And My ItemControl.Template consist of DataGrid. On running it successfully displays no of DataGrids binded to each element in Main List. Now I am binding the SelectedItem property of DataGrid to SelectedItem property of CustomTable. I am able to do this also. now when I select a row and the setter of SelectedItem of customTable is called I am creating new instance of Child with predefined Main in it (For sample only, the actual logic is complex and is not related to the question). Now I want to bind my Child to RowDetailsTemplate of my selectesRow so that Nested dataGrids are displayed. This should be recurssive like, clicking a row in Child's DataGrid should also display Child->Child->Main. I am not able to achieve this and trying for last few days with no progress. Any other approach other then this is also welcomed.
Edit
XAML File
<Window x:Class="HierDataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tvcc="clr-namespace:HierDataGrid"
Title="MainWindow" Height="350" Width="525" xmlns:metro="http://schemas.codeplex.com/elysium">
<Window.Resources>
<DataTemplate x:Key="Nested">
<ItemsControl ItemsSource="{Binding DataContext.Tables, RelativeSource={RelativeSource AncestorType=DataGrid, Mode=FindAncestor}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataGrid CanUserAddRows="False" RowDetailsTemplate="{DynamicResource Nested}" ItemsSource="{Binding Main}" AutoGenerateColumns="True" >
</DataGrid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</Window.Resources>
<ScrollViewer DataContext="{Binding}">
<StackPanel>
<ItemsControl ItemsSource="{Binding Path=TableCollection.Main}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataGrid Name="dg" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" CanUserAddRows="False" RowDetailsTemplate="{StaticResource Nested}" ItemsSource="{Binding}" AutoGenerateColumns="True" >
</DataGrid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>


TableCollectionis of typeCustomTable? - pushpraj