zoukankan      html  css  js  c++  java
  • 转载: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(如ListBoxListView)的 ItemContainerGenerator,以便只为可见项创建 UI 元素。

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

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

    <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; } }
    }

  • 相关阅读:
    简单马周游问题1152siicly
    Sicily 1155. Can I Post the lette
    POJ 1007
    给定2个大小分别为n, m的整数集合,分别存放在两个数组中 int A[n],B[m],输出两个集合的交集。
    算法1--
    GAN
    为什么ConcurrentHashMap是弱一致的
    手写HASHMAP
    千万级规模高性能、高并发的网络架构经验分享
    Web 通信 之 长连接、长轮询(转)
  • 原文地址:https://www.cnblogs.com/bear831204/p/1345644.html
Copyright © 2011-2022 走看看