VirtualizingStackPanel拒绝虚拟化

时间:2014-03-14 12:48:28

标签: c# wpf ui-virtualization

我编写了一个实现虚拟化的自定义面板。当放在ListBox中时,一切正常。

但是,如果我删除我的面板并使用默认的VirtualizingStackPanel,则在ListBox或重新模板化以支持虚拟化的ItemsControl中,控件不会虚拟化。

虚拟化 工作的示例:

<ListBox ItemsSource="{Binding Items}" ScrollViewer.CanContentScroll="True">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <CustomVirtualizingPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

虚拟无法工作的示例:

<ListBox ItemsSource="{Binding Items}" VirtualizingStackPanel.IsVirtualizing="True"/>

<ListBox ItemsSource="{Binding Items}" ScrollViewer.CanContentScroll="True" VirtualizingStackPanel.IsVirtualizing="True">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel IsVirtualizing="True" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Template>
        <ControlTemplate TargetType="{x:Type ItemsControl}">
            <Border BorderThickness="{TemplateBinding BorderThickness}"
                    Padding="{TemplateBinding Padding}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    Background="{TemplateBinding Background}"
                    SnapsToDevicePixels="True">
                <ScrollViewer CanContentScroll="True">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </ScrollViewer>
            </Border>
        </ControlTemplate>
    </ItemsControl.Template>
</ItemsControl>

控件直接放在Window中。为什么VirtualizingStackPanel不起作用?

1 个答案:

答案 0 :(得分:0)

获取第三个非工作示例。 我在自己的一个项目中使用这种风格并且正在工作。

<Style x:Key="VirtualizedItemsControl"
   TargetType="{x:Type ItemsControl}">
<Setter Property="VirtualizingStackPanel.IsVirtualizing"
        Value="True" />
<Setter Property="ScrollViewer.CanContentScroll"
        Value="True" />
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate>
            <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
                    Padding="{TemplateBinding Control.Padding}"
                    BorderBrush="{TemplateBinding Border.BorderBrush}"
                    Background="{TemplateBinding Panel.Background}"
                    SnapsToDevicePixels="True">
                <ScrollViewer Padding="{TemplateBinding Control.Padding}"
                              Focusable="False">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                </ScrollViewer>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
    <Setter.Value>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </Setter.Value>
</Setter>

我无法看到巨大的差异,但也许它会帮助你。