WPF DataGrid with TreeView type hierarchy

2019-06-06 08:58发布

Is it possible to have a pure hierarchical wpf data grid? Now there are 3 possible solutions dicussed over the internet ...

  1. GroupStyle
  2. TreeListView instead of DataGrid
  3. RowDetailsTemplate

Now all 3 are not useful in my case as they each have limitations.

GroupStyle provides grouped row presenters but their parent is simply GroupItem. This is where my data source is different. In my case the parent of the grouped items will be of the same type of item.

e.g. Folder class has List<Folders> children. So the parent of child folders is of type Folder itself.

RowDetailsTemplate would need me to host another datagrid that is bound to Children, but that would mean it would have its own column headers and what I want is the children to share the same column view of the parent DataGrid, just like they do in TreeListView.

And for TreeListView, thats what I am using right now, but its problem is ...

It looses virtualization and it is not a datagrid

:-)

I was thinking like some attached behavior that toggles the hierarchy view for the data grid like...

  <DataGrid HierarchicalBehavior.HierarchyPath="Children" ... />

Where Children is a property of type IEnumerable under each item.

I hope you guys understand what I am getting at.

Any ideas?

1条回答
聊天终结者
2楼-- · 2019-06-06 09:41

I could implement this finally. The code is too big to post. But if anyone's looking for the solution, do send an email to me.

Bascially I did the following things...

  1. I used the quick flattening of hierarchy of item into flat list.
  2. Each item bound to the WPF data grid I implemented INotifyPropertyChanged and also following extra properties via a base class ..

Parent => Parent item which this item is child for. Readonly.

HasHierarchy => False when hierarchy children is null or empty. Writeable.

IsItemVisible => Current Visibility. Writeable.

IsBranchVisible => False if any ancestor has IsExpanded = false or IsItemVisible = false. ReadOnly but notifyable.

IsExpanded => If the item is expanded. Writeable.

3. I put a PLINQ call to filter the `IsBranchVisible` property as true, of all children whenever any descenden's `IsExpanded` property is **toggled**.

Voala! It works like charm. Fast due to PLINQ.

查看更多
登录 后发表回答