虚拟化一个 ItemsControl? [英] Virtualizing an ItemsControl?

查看:32
本文介绍了虚拟化一个 ItemsControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ItemsControl 包含我想要虚拟化的数据列表,但是 VirtualizingStackPanel.IsVirtualizing="True" 似乎不适用于 ItemsControl.

I have an ItemsControl containing a list of data that I would like to virtualize, however VirtualizingStackPanel.IsVirtualizing="True" does not seem to work with an ItemsControl.

这是真的吗,还是有其他我不知道的方法?

Is this really the case or is there another way of doing this that I am not aware of?

为了测试我一直在使用以下代码块:

To test I have been using the following block of code:

<ItemsControl ItemsSource="{Binding Path=AccountViews.Tables[0]}"
              VirtualizingStackPanel.IsVirtualizing="True">
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <TextBlock Initialized="TextBlock_Initialized"  
                   Margin="5,50,5,50" Text="{Binding Path=Name}" />
    </DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

如果我将 ItemsControl 更改为 ListBox,我可以看到 Initialized 事件只运行了几次(巨大的边距只是所以我只需要浏览一些记录),但是作为 ItemsControl 每个项目都会被初始化.

If I change the ItemsControl to a ListBox, I can see that the Initialized event only runs a handful of times (the huge margins are just so I only have to go through a few records), however as an ItemsControl every item gets initialized.

我尝试将 ItemsControlPanelTemplate 设置为 VirtualizingStackPanel 但这似乎没有帮助.

I have tried setting the ItemsControlPanelTemplate to a VirtualizingStackPanel but that doesn't seem to help.

推荐答案

实际上不仅仅是让 ItemsPanelTemplate 使用 VirtualizingStackPanel.ItemsControl 的默认 ControlTemplate 没有 ScrollViewer,这是虚拟化的关键.添加到 ItemsControl 的默认控件模板(使用 ListBox 的控件模板作为模板)给我们以下内容:

There's actually much more to it than just making the ItemsPanelTemplate use VirtualizingStackPanel. The default ControlTemplate for ItemsControl does not have a ScrollViewer, which is the key to virtualization. Adding to the the default control template for ItemsControl (using the control template for ListBox as a template) gives us the following:

<ItemsControl ItemsSource="{Binding AccountViews.Tables[0]}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <TextBlock Initialized="TextBlock_Initialized"
                 Text="{Binding Name}" />
    </DataTemplate>
  </ItemsControl.ItemTemplate>

  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <VirtualizingStackPanel IsVirtualizing="True"
                              VirtualizationMode="Recycling" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>

  <ItemsControl.Template>
    <ControlTemplate TargetType="ItemsControl">
      <Border BorderThickness="{TemplateBinding BorderThickness}"
              BorderBrush="{TemplateBinding BorderBrush}"
              Background="{TemplateBinding Background}">
        <ScrollViewer CanContentScroll="True" 
                      Padding="{TemplateBinding Padding}"
                      Focusable="False">
          <ItemsPresenter />
        </ScrollViewer>
      </Border>
    </ControlTemplate>
  </ItemsControl.Template>
</ItemsControl>

(顺便说一句,查看默认控件模板的一个很好的工具是显示模板)

(BTW, a great tool for looking at default control templates is Show Me The Template)

注意事项:

你必须设置ScrollViewer.CanContentScroll=True",见这里 为什么.

You have to set ScrollViewer.CanContentScroll="True", see here for why.

另请注意,我放置了 VirtualizingStackPanel.VirtualizationMode=Recycling".这将减少 TextBlock_Initialized 被调用的次数,但是许多 TextBlocks 在屏幕上都是可见的.您可以在 此处阅读有关 UI 虚拟化的更多信息.

Also notice that I put VirtualizingStackPanel.VirtualizationMode="Recycling". This will reduce the numbers of times TextBlock_Initialized is called to however many TextBlocks are visible on the screen. You can read more on UI virtualization here .

忘记说明显而易见的:作为替代解决方案,您可以将 ItemsControl 替换为 ListBox :)另外,请查看此 优化 MSDN 页面上的性能 并注意 ItemsControl 不在实现性能功能的控件"中.table,这就是为什么我们需要编辑控件模板.

Forgot to state the obvious: as an alternate solution, you can just replace ItemsControl with ListBox :) Also, check out this Optimizing Performance on MSDN page and notice that ItemsControl isn't in the "Controls That Implement Performance Features" table, which is why we need to edit the control template.

这篇关于虚拟化一个 ItemsControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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