【发布时间】: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