如何通过重新定义的 ListBox 模板使用 UI 虚拟化 [英] How to use UI virtualization with redefined ListBox templates

查看:16
本文介绍了如何通过重新定义的 ListBox 模板使用 UI 虚拟化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆