转载:VirtualizingStackPanel

原文http://blogs.msdn.com/vinsibal/archive/2008/05/14/recycling-that-item-container.aspx

Recycling that Item Container

Item container recycling is a one of the many new features in SP1.  I'm going to talk a little bit about it here.

What is it?

As a little background, the VirtualizingStackPanel's virtualization scheme basically works like this: generate containers when needed and throw them away when they are no longer in view.  This does solve the memory issue that is created when the panel has a large number of items, but it can still be very expensive to have to recreate and throw away containers every time they go out of view.  Enter container recycling.  Container recycling is a performance optimization to this virtualization scheme.  It basically reuses existing containers so they do not have to be recreated each time they come back into view.

How do I use it?
 
VirtualizingStackPanel's virtualization will still work the same as before.  In fact container recycling will be turned off by default.  I will explain how to turn it on shortly, but before that let me describe the new API.  VSP (VirtualizingStackPanel) has added a new attached property, VirtualizationModeProperty, as well as the getter and setter for it, GetVirtualizationMode and SetVirtualizationMode.  This property is used to describe the type of mode that it can be in, VirtualizationMode.Standard or VirtualizationMode.Recycling.  The former will use the standard virtualization scheme just as it normally would and the latter will include recycling to the virtualization scheme.  The VirtualizationModeProperty is to be set from the ItemsControl that hosts the items presented by the VSP.  By default, it is set to VirtualizationMode.Standard.

Here is an example of turning on recycling on a ListBox:

<ListBox VirtualizingStackPanel.VirtualizationMode="Recycling" …/>

Note that ListBox uses a VSP as its ItemsPanel and sets the VirtualizingStackPanel.IsVirtualizing attached property to true by default.  If you were using ItemsControl directly or creating a custom ItemsControl that uses a VSP as its ItemsPanel, you will need to set both attached properties to get the recycling behavior.

<ItemsControl VirtualizingStackPanel.IsVirtualizing="true" VirtualizingStackPanel.VirtualizationMode="Recycling" …/>

Special considerations

Just as with the virtualization scheme, marking containers as non-virtualizable will not virtualize or recycle the container.  You can do this by handling VSP's CleanUpVirtualizedItem event.  This event is fired when a container is about to be thrown away or recycled.  You have the option here of cancelling the operation.

Some other helpful methods for you to have more control of the data items and containers include ItemsControl.PrepareContainerForItemOverride which is called before a container is used and ItemsControl.ClearContainerForItemOverride which is called when a container is thrown away or recycled.

For more information regarding scrolling performance take a look at this article by one our dev's here on the WPF team.  It came out before this feature so the information regarding container recycling is a suggestion on how to implement it and not how to actually use it.  It still has a lot of great information though.

Posted: Wednesday, May 14, 2008 6:07 PM by vinsibal

VirtualizingStackPanel类

下面是msdn上的说明:

标准布局系统可以创建项容器并为每个与列表控件关联的项计算布局。"虚拟化"是指一种技术,通过该技术,可根据屏幕上所显示的项来从大量数据项中生成用户界面 (UI) 元素的子集。如果在可能只有少量元素显示在屏幕上时生成多个 UI 元素,则会对应用程序的性能产生负面影响。VirtualizingStackPanel 可以计算可见项的数量,

并处理来自ItemsControl(如ListBox或ListView)的 ItemContainerGenerator,以便只为可见项创建 UI 元素。

仅当StackPanel中包含的项控件创建自己的项容器时,才会在该面板中发生虚拟化。可以使用数据绑定来确保发生这一过程。如果创建项容器并将其添加到项控件中,则与StackPanel相比,VirtualizingStackPanel 不能提供任何性能优势。

下面是ListBox默认的ItemPanelTemplate,可以看到,VirtualizingStackPanel是ListBox元素的默认项宿主:

<ItemsPanelTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">                              <VirtualizingStackPanel IsItemsHost="True" /></ItemsPanelTemplate>

默认情况下,IsVirtualizing属性设置为 true。 当IsVirtualizing设置为 false 时,VirtualizingStackPanel 的行为与普通StackPanel一样。

例如在ListBox中这样用它:

<ListBox VirtualizingStackPanel.IsVirtualizing="True"

ItemsSource="{Binding XPath=Team}"

ItemTemplate="{DynamicResource NameDataStyle}"/>

对于comboBox, 它默认的ItemsPanel是StackPanel,用VirtualizingPanel能提高性能,如下面的例子:

<Window x:Class="WpfApplication67.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="350" Width="525"        xmlns:local="clr-namespace:WpfApplication67"><Grid><ComboBox Name="cb_virtualizing" Width="200" Height="23" ItemsSource="{x:Static local:MainWindow.Families}" Margin="12,139,290,149"><ComboBox.ItemsPanel><ItemsPanelTemplate><VirtualizingStackPanel /></ItemsPanelTemplate></ComboBox.ItemsPanel></ComboBox><ComboBox Name="cb_normal" Width="200" Height="23" ItemsSource="{x:Static local:MainWindow.Families}" Margin="235,139,68,149"></ComboBox></Grid></Window>

public partial class MainWindow : Window    {public MainWindow()        {            InitializeComponent();        }

private static readonly ICollection<FontFamily> _families = GetFamilies();private static ICollection<System.Windows.Media.FontFamily> GetFamilies()        {            Collection<FontFamily> r = new Collection<FontFamily>();for (int i = 0; i < 20; i++)            {foreach (var v in Fonts.SystemFontFamilies)                    r.Add(v);            }return r;        }

public static ICollection<FontFamily> Families { get { return _families; } }    }

posted on 2008-12-02 12:03 cutebear 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/bear831204/archive/2008/12/02/1345644.html

转载:VirtualizingStackPanel相关推荐

  1. [转载]Tensorflow 的reduce_sum()函数的axis,keep_dim这些参数到底是什么意思?

    转载链接:https://www.zhihu.com/question/51325408/answer/125426642 来源:知乎 这个问题无外乎有三个难点: 什么是sum 什么是reduce 什 ...

  2. u-boot中filesize环境变量【转载】

    转载地址:https://blog.csdn.net/fzs333/article/details/48518559 U-Boot中的环境命令可以使用$(filesize)来确定刚下载(传输)得到的文 ...

  3. linux常用命令(转载)

    Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因,比较短 ...

  4. makefile学习(转载)

    该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客: http://blog.csdn.net/haoel/article/details/2886 makefile很重 ...

  5. 浅谈UWB(超宽带)室内定位技术(转载)

    技术背景 随着无线通信技术的发展和数据处理能力的提高,基于位置的服务成为最有前途的互联网业务之一.无论移动在室内还是室外环境下,快速准确地获得移动终端的位置信息和提供位置服务的需求变得日益迫切.通信和 ...

  6. 【转载】浏览器事件循环机制(event loop)

    首先,本文转自https://juejin.im/post/5afbc62151882542af04112d 当我看完菲利普·罗伯茨的 javascript event loop的演讲的时候,就对于事 ...

  7. [转载]Surging 分布式微服务框架使用入门

    前言 本文非 Surging 官方教程,只是自己学习的总结.如有哪里不对,还望指正. 我对 surging 的看法 我目前所在的公司采用架构就是类似与Surging的RPC框架,在.NET 4.0框架 ...

  8. 负二项分布学习[转载]

    转自:https://wenku.baidu.com/view/b6518ea5112de2bd960590c69ec3d5bbfd0adaff.html 1.伯努利实验 2.二项分布 3.负二项分布 ...

  9. 【转载】tf.nn.conv2d_transpose是怎样实现反卷积的?

    原文链接:https://blog.csdn.net/mao_xiao_feng/article/details/71713358 转载于:https://www.cnblogs.com/Virtua ...

  10. Js时间格式[转载]

    var myDate = new Date(); myDate.getYear();        //获取当前年份(2位) myDate.getFullYear();    //获取完整的年份(4位 ...

最新文章

  1. 《CLR Via C# 第3版》笔记之(十四) - 泛型高级
  2. UE if缩进问题(转)
  3. MySQL复习资料(六)——MySQL-多表联合查询
  4. 灯也能玩出花样?炫彩灯带!
  5. PreparedStatement预编译的sql执行对象
  6. C# 实现软件自动更新升级程序
  7. python-容器数据类型-知识小结
  8. css开头的文本格式,css文件开头怎么写
  9. webservice实例java_Java WebService(实战) 简单实例
  10. java 调查问卷_java学习调查问卷
  11. usb摄像头android录像软件,USB摄像头app
  12. 精密电阻(1%) -- 贴片电阻常用标示方法(转载)
  13. 最强大脑记忆曲线(5)——主程序设计
  14. PG-Strom源码分析
  15. 人人都是架构师:分布式系统架构落地与瓶颈突破 - china-pub网上书店
  16. 语言处理 之 melgan
  17. 提升线下实体店铺购物体验
  18. win32 010 使用masm32
  19. pfx证书导入方式,步骤流程
  20. jquery.选择器

热门文章

  1. rmd文件怎么转换html文件,如何将Rmd文件的html输出向左对齐
  2. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_09-修改页面-服务端-接口开发...
  3. ActiveMQ学习笔记(1)----初识ActiveMQ
  4. 分子量 (Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)
  5. Unity UGUI —— 鼠标穿透UI问题(Unity官方的解决方法)
  6. 实现gabor filter的滤波
  7. VC 中一些控件的使用方法(TabControl, 工具栏)
  8. Mysql ORDER BY用法的一点理解
  9. 适应adblock plus 规则的简单正则表达式匹配
  10. VC++动态链接库编程之MFC规则DLL