样式TreeView但保持虚拟化

时间:2011-10-02 23:03:46

标签: wpf treeview styles virtualization

TreeView 上的虚拟化在我只使用样式中的xaml样式设置 TreeViewItem 样式时起作用:

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

但是如果我尝试给 TreeView 本身一个带有 Style = {Resource} 的样式,那么虚拟化会中断,即:

<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>

提前致谢!

2 个答案:

答案 0 :(得分:4)

通过在ScrollViewer上指定CanContentScroll =“False”,您将始终停止虚拟化

查看why setting ScrollViewer.CanContentScroll to false disable virtualization

如果仍有问题,请仔细检查在Snoop或WPF Inspector中创建的可视树。要使虚拟化工作,IScrollInfo对象(即面板)必须是ScrollViewer的直接子代。

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

希望有所帮助, 标记

答案 1 :(得分:0)

MarkDaniel alredy回答(谢谢!)但这是一个完整的示例风格:

<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>