1

The Virtualization on my TreeView works when I have only styled the TreeViewItem with this bit of xaml in the style:

<Style.Triggers>
    <Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Trigger>
</Style.Triggers>

But then if I try and give the TreeView itself a style with Style={Resource} the virtualization breaks, i.e:

<Style x:Key="FontTreeViewStyle" TargetType="{x:Type TreeView}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeView">
                <Border Name="Border" CornerRadius="1" BorderThickness="1" BorderBrush="{DynamicResource BorderMediumColor}" Background="{DynamicResource ControlLightColor}">
                    <ScrollViewer Focusable="False"  CanContentScroll="False" Padding="4"><!-- Style="{StaticResource MyScrollViewer}"-->
                        <ItemsPresenter />
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Thanks in advance!

Dominic
  • 62,658
  • 20
  • 139
  • 163
  • Fixed. I copied the style generated in Expression Blend and realised I had not fully implemented VirtualizingStackPanel.IsVirtualizing – Dominic Oct 09 '11 at 22:44

2 Answers2

4

By specifying CanContentScroll="False" on the ScrollViewer you will always stop it virtualizing

Take a look at why setting ScrollViewer.CanContentScroll to false disable virtualization

If you still have problems double check the visual tree that's created in something like Snoop or WPF Inspector. For virtualization to work the IScrollInfo object (ie the panel) must be the direct child of the ScrollViewer.

http://msdn.microsoft.com/en-us/library/ms750665.aspx

Hope that helps, Mark

Community
  • 1
  • 1
MarkDaniel
  • 147
  • 2
  • 10
  • Did it help? I have the same problem: when I specify a style for my TreeView the virtualization breaks. – Shakaron Jul 13 '15 at 15:32
  • Worked for me, thanks! (The CanContentScroll property defaults to false if unset, you have to set it to a value/true) – J. Andersen Apr 29 '18 at 12:52
0

MarkDaniel alredy answered (thanks!) but here is a full example style:

<Style x:Key="ScrollViewerStyle" TargetType="{x:Type ScrollViewer}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <ScrollContentPresenter CanContentScroll="True" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.IsContainerVirtualizable="True" VirtualizingPanel.VirtualizationMode="Recycling"/>

                        <ScrollBar Name="PART_VerticalScrollBar"
                            Grid.Column="1"
                            Value="{TemplateBinding VerticalOffset}"
                            Maximum="{TemplateBinding ScrollableHeight}"
                            ViewportSize="{TemplateBinding ViewportHeight}"
                            Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                            Style="{DynamicResource ScrollBarStyle}"/>
                        <ScrollBar Name="PART_HorizontalScrollBar"
                            Orientation="Horizontal"
                            Grid.Row="1"
                            Value="{TemplateBinding HorizontalOffset}"
                            Maximum="{TemplateBinding ScrollableWidth}"
                            ViewportSize="{TemplateBinding ViewportWidth}"
                            Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                            Style="{DynamicResource ScrollBarStyle}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
J. Andersen
  • 161
  • 1
  • 5