wpfvirtualizingstackpanelitemspresenter

Is it possible to override the ItemsPresenter to use a Virtualizing StackPanel instead of a regular stack panel?


Background

I have a custom control that inherits from a TreeView and is modified to display in a data grid style. The problem I am seeing is with performance when expanding the tree. This is common from my research with Tree Views. Upon inspection with the WPF Performance tools I noticed that the ItemsPresenter class is using a regular Stack Panel instead of a Virtualizing Stack Panel.

enter image description here

Here is the section of code where the ScrollContentPresenter is used (showing in image).

<ScrollContentPresenter Name="PART_ScrollContentPresenter"
      KeyboardNavigation.DirectionalNavigation="Local"
      Content="{TemplateBinding Content}"
      ContentTemplate="{TemplateBinding ContentTemplate}"
      CanContentScroll="{TemplateBinding CanContentScroll}"
      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>

Here is the template being passed in.

    <ControlTemplate TargetType="CommonControls:TreeListViewItem508">
    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Border x:Name="item">
            <Border Name="InnerBorder">
                <Grid Style="{StaticResource GridBackgroundStyle}">
                    <Rectangle Visibility="Collapsed" Fill="#75FFFFFF" Name="UpperHighlight" />
                </Grid>
            </Border>
        </Border>
        <ItemsPresenter Grid.Row="1" Name="ItemsHost" />
    </Grid>
</ControlTemplate>

Question

Is it possible to force the items presenter to use a virtualizing stack panel?

Notes

Any suggestions or options are much appreciated.

Resolved:

I modified the template's style by adding this to the style and it switched the stack panels to virtualizing.

 <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>

Solution

  • Try

          <TreeView>
            <TreeView.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel/>
                </ItemsPanelTemplate>
            </TreeView.ItemsPanel>
        </TreeView>
    

    or

    <TreeView VirtualizingStackPanel.IsVirtualizing="True">
    

    Obviosuly replace TreeView, with your treeview control name.

    Hope that helps

    Paul