Arsen Zahray
Arsen Zahray

Reputation: 25287

Limit how deep TreeView would go when expanding TreeNode in WPF

I'm using WPF TreeView bound to my classes though MVVM to display a large array of multilevel data. Because of this, when I expand TreeNode, it takes some time and a lot of memory.

I think, that the memory requirement comes from TreeView scouting out nodes down the line, not just the first generation of the children, forcing those nodes to load data.

Is there any way to limit this "scouting" behavior to only the first generation of child nodes?

Upvotes: 4

Views: 1359

Answers (2)

GemBox Dev Team
GemBox Dev Team

Reputation: 669

Check if TreeView is using VirtualizingStackPanel. I believe that it is used by default in .NET 4.0, but you must set it explicitly in previous .NET versions.

For more information, see How to: Improve the Performance of a TreeView.

Upvotes: 1

Marat Khasanov
Marat Khasanov

Reputation: 3848

WPF generates TreeViewItem(s) only when they are displayed, i.e. no item is generated until its Parent expanded. So, I think the problem is not a "scouting".

Anyway, you may try to implement dynamic loading in your ViewModel by adding synchronization with TreeViewItem.IsExpanded using TwoWay Binding. Take a look at this. You may use the same solution for IsExpanded property. Don't forget to add fake item to tell TreeViewItem that it is expandable.

Upvotes: 2

Related Questions