WPF: DataGrid with a tree-column in an MVVM Window

2019-08-30 19:46发布

问题:

Here is a rephrased version of my issue:

Here is a screenshot of the result I'm looking for (from an older, non C#/WPF version of the application):

Looks like all the other grid-tree-views and what ObjectListView provides. But unfortunately I have some rules I have to follow:

  • WPF
  • MVVM
  • Just a DataGrid and its components must be used (no TreeView, ListView, etc.)
  • The Items are hold in an ObservableCollection<T> where T.Property1, T.Property2 etc hold the data for the columns of the DataGrid.
  • The lower Levels of an item are hold in T.PropertyChilds of type ObservableCollection<T> and are not known/filled beforehand. They must be loaded on the first exand of the item.

Here is a screenshot of similar data and what I've got so far in WPF/C#:

It looks like a standard DataGrid. But which columns it can hold, which property of T the columns are showing, which columns are currently visible, etc is defined by a own framework around the DataGrid which basically just adds columns to the public ObservableCollection<DataGridColumn> Columns Collection of a System.Windows.Controls.DataGrid.

My task is now to do a new class derived from DataGridColumn which can display and load the hierarchical data structure of my ViewModel described above.

The main problems I'm facing are as far as I can tell: - how to visibly display the levels like a treeview in a DataGrid column - how to process the loaded data from childs so they are shown as in the first screenshot above. Do I have to do a flat ObservableCollection<T> where I add the loaded ChildData and some extra properties like level and parent - or is it possible to define a kindo of a hierarchical data template for a DataGrid?

Thanks a lot Soko