Bind dynamic data with wpf datagrid inside rowdetails template

I have a datagrid, say Datagrid1, which is populated with a list (dynamically through the code behind). Inside the row data template for a datagrid, I want to add a datagrid, lets call it datagrid2, does datagrid2 need to be dynamically replenished using the List on the SelectionChange event Datagrid1? Access to datagrid2 and its binding to the data source must be done in code. Can anyone help me with this? my xaml:

<Grid> <my:DataGrid Name="dataGrid1" ItemsSource="{Binding}"> <my:DataGrid.RowDetailsTemplate> <DataTemplate> <my:DataGrid Name="dataGrid2"></my:DataGrid> </DataTemplate> </my:DataGrid.RowDetailsTemplate> </my:DataGrid> </Grid> 
+4
source share
2 answers

It would be much easier to do this with bindings. You can add a DetailElements collection to each item in your ItemsGrid1 ItemsSource. Now all you have to do is bind this collection to your ItemsGrid2 'ItemsSource, and it is automatically populated with data through the binding.

 public class DataGrid1SourceItem { public ObservableCollection<DetailItem> DetailItems {get;set;} } 

XAML:

 <Grid> <my:DataGrid Name="dataGrid1" ItemsSource="{Binding}"> <my:DataGrid.RowDetailsTemplate> <DataTemplate> <my:DataGrid Name="dataGrid2" ItemsSource="{Binding Path=DetailItems}"></my:DataGrid> </DataTemplate> </my:DataGrid.RowDetailsTemplate> </my:DataGrid> </Grid> 

EDIT: to search your database depending on the value of the DataGrid cells you need to get this value in your ViewModel. To do this, create a property (in my example ProductName ) and bind it to the DataGridColumn Binding property (Mode = TwoWay). Then you can have a private field that contains all the products and filters this field in the ItemsGrid2 ItemsSources data collection:

 public class DataGrid1SourceItem { private List<DetailItems> _allDetailItems = new List<DetailItems>(); public IEnumerable<DetailItem> DetailItems { get { return _allDetailItems.Where(item => item.Name == ProductName); } } public DataGrid1SourceItem() { // load your products into _allDetailItems } private string _productName; public string ProductName { get { return _productName; } set { _productName= value; OnPropertyChanged("ProductName"); OnPropertyChanged("DetailItems"); } } } 
+6
source

The following may help you

 public partial class Window1 : Window { DataTable dt = new DataTable(); public Window1() { InitializeComponent(); dt.Columns.Add("Num1", typeof(string)); dt.Columns.Add("Num2", typeof(string)); dt.Rows.Add("100", "200"); dt.Rows.Add("300", "400"); this.dataGridTest.DataContext = dt; this.dataGridTest.RowDetailsVisibilityChanged += new EventHandler<Microsoft.Windows.Controls.DataGridRowDetailsEventArgs>(dataGridTest_RowDetailsVisibilityChanged); } void dataGrid1_RowDetailsVisibilityChanged(object sender, Microsoft.Windows.Controls.DataGridRowDetailsEventArgs e) { Microsoft.Windows.Controls.DataGrid innerDataGrid = e.DetailsElement as Microsoft.Windows.Controls.DataGrid; innerDataGrid.ItemsSource = ((IListSource)dt).GetList(); } } 

In xaml

 <Grid> <my:DataGrid Name="dataGridTest" ItemsSource="{Binding}"> <my:DataGrid.RowDetailsTemplate> <DataTemplate> <my:DataGrid Name="innerGrid"></my:DataGrid> </DataTemplate> </my:DataGrid.RowDetailsTemplate> </my:DataGrid> </Grid> 
+3
source

Source: https://habr.com/ru/post/1501999/


All Articles