How would you go about binding a WPF datagrid that needs to display data from many different datasources with varying number of columns with different column headers and types?
What I'm currently doing is creating a custom List<DataGridColumn>()
list of datagrid columns in my ViewModel for each different collection of records to be displayed in this DataGrid.
I Loop over this List to set the DataGrid columns:
foreach (DataGridColumn dgc in dgcSample)
{
dgc.HeaderStyle = hStyle;
dgMyDataGrid.Columns.Add(dgc);
}
Finally, I use the ItemsSource to set the source of items:
dgMyDataGrid.ItemsSource = SomeCollection;
This works but it is not binding and it breaks the MVVM guideline that ViewModel should be agnostic to specific UI elements since it now has to deal with the DataGrid
and host a collection of DataGridColumn
objects...
Any thoughts?