【问题标题】:VirtualizingStackPanel refuses to virtualizeVirtualizingStackPanel 拒绝虚拟化
【发布时间】:2014-03-14 12:48:28
【问题描述】:

我编写了一个实现虚拟化的自定义面板。当放置在 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>

控件直接放置在窗口内。为什么 VirtualizingStackPanel 不起作用?

【问题讨论】:

  • 您如何验证Virtualizing 是否有效?
  • @RohitVats 控件加载大约需要 10 秒,尝试滚动需要类似的时间,并且该时间与项目源中包含的项目数量成比例。
  • 重要的是where 您是否放置了这些 UI 元素。例如,如果你把这个ListBox 放在StackPanel 里面,它就不会被虚拟化。发布这些元素的使用示例。
  • @HighCore ListBox 直接在Window 内部,没有其他控件。

标签: c# wpf ui-virtualization


【解决方案1】:

获得您的第三个无效示例。 我在我自己的一个项目中使用这种风格并且它正在工作。

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

我看不出有很大的不同,但也许它会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2013-06-30
    • 2021-03-15
    • 2015-11-19
    • 2012-09-11
    • 1970-01-01
    相关资源
    最近更新 更多