Is it possible in WPF to have a Datagrid with some common functionality for different itemsources? Can we have a Datagrid in a control which can accept any observable collection. is there a way we can pass the Column information or define column template to show only certain columns with their header and bindings passed to the control containing the Datagrid? Rest of the functionalities like add. delete, sort, clicks will be using Commnds and will be common for all itemSources. The only thing hindering is the way I can define columns for this Datagrid? If it is possible to pass the Datagrid.Columns to the control as template or any other way.
Asked
Active
Viewed 146 times
2 Answers
3
You will need to handle the DataGrid.AutoGeneratingColumn event, then set the column's Visibility property according to your needs: https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.datagrid.autogeneratingcolumn?view=net-5.0

Julius
- 187
- 5
-
This does break the MVVM paradigm, but the WPF DataGrid predates MVVM by a few years and wasn't designed with MVVM in mind. – Julius Apr 23 '21 at 18:02
0
You write data to DataTable and pass the table to DataGrid.ItemsSource. For simple data (int, double, string, etc.) this will be enough.

EldHasp
- 6,079
- 2
- 9
- 24