WPF TreeView Memory Consumption and Performance
One very simple thing you can do that can significantly reduce the memory consumption and drastically improve performance of a WPF TreeView is to set the following two properties. In XAML, you would do:
<TreeView x:Name=”myTreeView”
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling" />
In a simple test of a data-bound TreeView containing just 5000 items, turning on stack panel virtualization reduces the memory overhead from over 110MB to about 2MB. Additionally, the control also gets a performance boost in items’ expand/collapse, selection, scrolling, and general navigation.
The WPF ListView also support these properties but, unlike the TreeView, its VirtualizingStackPanel.IsVirtualizing is already default to True.
Enjoy.
-Tan
Comments
Anonymous
December 09, 2010
Thanks for this one Tan! You made my day. Still really slow filling it up dynamically, but at least the performance for opening and closing a branch is greatly improved.Anonymous
December 07, 2012
Thank you!!!! )))) It's very help me!Anonymous
August 05, 2015
Awesome tip! Couldn't figure out why our TreeView was sluggish. This fixed it!