wpf 如何在重新定义的 ListBox 模板中使用 UI 虚拟化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28354139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use UI virtualization with redefined ListBox templates
提问by yaapelsinko
I'm trying to use ListBox as a view containing multiple items and, of course, I need to use UI virtualization in it.
我正在尝试使用 ListBox 作为包含多个项目的视图,当然,我需要在其中使用 UI 虚拟化。
The problem is virtualization works only when I declare ListBox this way:
问题是虚拟化仅在我以这种方式声明 ListBox 时才有效:
<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>
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?
据我发现,这个示例包含的内容与默认情况下 ListBox 包含的内容相同。但是虚拟化不起作用。我在这里阅读了几篇文章和几个答案,但仍然无法弄清楚“一般方法” - 我必须设置、绑定、添加什么和在哪里才能使虚拟化与自定义模板一起工作?
回答by Adi Lester
The reason is that you're using a StackPanelfor your ItemsPanel- you should be using a VirtualizingStackPanelinstead (which is also the default ItemsPanel for ListBox).
其原因是,您使用的是StackPanel你ItemsPanel-你应该使用VirtualizingStackPanel,而不是(这也是默认的ItemsPanel的列表框)。
Either remove your ItemsPaneldefinition or modify it to use a VirtualizingStackPanel:
删除您的ItemsPanel定义或修改它以使用VirtualizingStackPanel:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
回答by d.moncada
Two things:
两件事情:
Update your PanelTemplateto use a VirtualizingStackPaneland add your virtualizationoptions to the ScrollViewerof the ControlTemplate.
更新您的PanelTemplate使用VirtualizingStackPanel,并添加您virtualization选择到ScrollViewer的ControlTemplate。
<ListBox.Template>
<ControlTemplate>
<ScrollViewer VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<views:SiteEntryView />
</DataTemplate>
</ListBox.ItemTemplate>

