Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF : TreeView virtualization not working

What can stop a TreeView from virtualizing if the TreeView is set up as follows?

<TreeView 
    ItemsSource="{Binding}" 
    VirtualizingStackPanel.IsVirtualizing="True">
    <TreeView.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </TreeView.ItemsPanel>
    <TreeView.ItemContainerStyle>
        <Style
            TargetType="{x:Type TreeViewItem}">
            <Setter
                Property="IsExpanded"
                Value="{Binding IsExpanded, Mode=TwoWay}"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

I have one that is not virtualizing, when i expand the nodes (and use snoop to check) i have all of the TreeViewItems being created. I am wondering if there is some combination of containers that would prevent the TreeView from virtualizing its content. (like hosting it in a StackPanel for example)

like image 826
Aran Mulholland Avatar asked Nov 22 '10 00:11

Aran Mulholland


2 Answers

The problem was with the styling. After some research we found that there was an unnamed style targeting the TreeView (i.e. one with DataType={x:Type TreeView} without an x:Key) and one targetting the TreeViewItem in our App.xaml (or equivalent) It was overriding the ControlTemplate for each respectively.

These styles did not have the triggers to set the ItemsPanel to a VirtualizingStackPanel and had no mention of any virtualization. When the styles are removed the TreeView works fine. Even though the local properties set the ItemsPanel and the VirtualizingStackPanel.Isvirtualizing="True" on the TreeView these properties were not being propogated to the TreeViewItems so the top level of the TreeView would virtualize whilst the sub categories would not (as their virtualization behaviour was dependant on the TreeViewItem)

like image 99
Aran Mulholland Avatar answered Sep 21 '22 10:09

Aran Mulholland


Had the same problem. In my case, the initial size of the TreeView was not limited (TreeView is within a popup). Therfore, the virtualization panel initialized all controls for the first time.

Setting the MaxHeigt of the TreeView to 1000 solvend the problem.

like image 44
JanDotNet Avatar answered Sep 18 '22 10:09

JanDotNet