如何通过重新定义的ListBox模板使用UI虚拟化 [英] How to use UI virtualization with redefined ListBox templates
问题描述
我试图将ListBox用作包含多个项目的视图,当然,我需要在其中使用UI虚拟化.
I'm trying to use ListBox as a view containing multiple items and, of course, I need to use UI virtualization in it.
问题在于,只有当我这样声明ListBox时,虚拟化才有效:
The problem is virtualization works only when I declare ListBox this way:
<ListBox
ItemsSource="{Binding ItemsSource}"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<ListBox.ItemTemplate>
<DataTemplate>
<views:SiteEntryView />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
但是,如果我尝试对其进行自定义,它将不再进行虚拟化:
But if I try to customize it, it doesn't virtualizing anymore:
<ListBox
ItemsSource="{Binding ItemsSource}"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<ListBox.Template>
<ControlTemplate>
<ScrollViewer>
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<views:SiteEntryView />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
据我发现,此示例包含的内容与ListBox默认包含的内容相同.但是虚拟化无法正常工作.我已经阅读了几篇文章,并在此处获得了一些答案,但是仍然无法弄清楚通用方式"-我必须设置,绑定,添加等什么地方才能使虚拟化与自定义模板一起使用?
As far as I've found, this sample contains just the same that ListBox contains by default. But virtualization isn't working. I've read several articles and also couple of answers here, but still can't figure out the "general way" - what and where I must set, bind, add, etc to make virtualization work with custom templates?
推荐答案
原因是您为ItemsPanel
使用了StackPanel
-您应该改用VirtualizingStackPanel
(这也是默认设置ListBox的ItemsPanel.)
The reason is that you're using a StackPanel
for your ItemsPanel
- you should be using a VirtualizingStackPanel
instead (which is also the default ItemsPanel for ListBox).
删除您的ItemsPanel
定义或将其修改为使用VirtualizingStackPanel
:
Either remove your ItemsPanel
definition or modify it to use a VirtualizingStackPanel
:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
这篇关于如何通过重新定义的ListBox模板使用UI虚拟化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!