Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49438
  • 博文数量: 21
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-24 15:25
文章分类

全部博文(21)

文章存档

2013年(21)

我的朋友

分类: C#/.net

2013-07-23 11:43:03

Adorner Example – TextBox Watermark and ItemsPanelTemplate

Topic: WPF – Virtualization – VirutalizationStackPanel and ItemsPanelTemplate

标题: WPF – 控件虚拟化- 简单介绍VirutalizationStackPanelItemsPanelTemplate

在ItemsPanel上的虚拟化, 你可以使用VirutalizationStackPanel, 和 “ItemsPanelTemplate”, 以下是一个虚拟化的例子.

Virtualization on the ItemsPanel, you can use the VirutalizationStackPanel, together with the “ItemsPanelTemplate”, here is a live use example of that .



               

                   

                       

                            cmbh:PixelBasedScrollingBehavior.IsEnabled="True"

                            VirtualizationMode="Recycling"/>

                   

               


我们将关注两点,一点是VirtualizingStackPanel,另一点是ItemsPanelTemplate

So we will focus on two parts, one is the VirtualizingStackPanel and the other is ItemsPanelTemplate


VirtualizingStackPanel

一般的来说,标准的布局系统为每一个元素创建元素容器和布局元素,虚拟化这个词的意思是指一种从大量的数据中,只显示用户界面可见一部分元素的技巧。如果只有少量的元素被显示,但生成许多UI元素的做法会影响到他的performance. VirtualizingStackPanel计算,控制ItemsControl (比如说ListBox 或ListView)的ItemContainerGenerator生成可视部分元素。

So generally, the standard layout system create item containers and layout for each item associate with a list control, the word “virtualize” refers to a technique by which a subset of user interface (UI) elements are generated from a larger number of data items based on which items are visible on-screen. Generating many UI elements when only a few elements might be on the screen can adversely affect the performance of your application. The VirtualizingStackPanel calculates the number of visible items and works with the ItemContainerGenerator from an ItemsControl (such as ListBox or ListView) to create UI elements only for Visible items.


虚拟化只是在panel里面的items control生成自己的item container.可以采用数据绑定的办法确保如此。如果item container是被显示的创立的话,那么virtualizingStackPanel比没用virtualizingStackPanel没有效率的优势。

Virtualization in a StackPanel only occurs when the items control contained in the panel creates its own item container. You can ensure this happen by using DataBinding . In Scenarios where item container are created and added to the items control. A virtualizingStackPanel offers no performance advantage over a StackPanel.


VirtualizingStackPanel是ListBox元素的默认元素容器,默认的,IsVirtualizing属性值是true.

VirtualizingStackPanel is the default items host for ListBox element, by default, the IsVirtualizing property is set to true.


IsVirtualizing属性值是false时,和不用VirtualizingStackPanel效果是一样的。

When isVirtualizing is set to false, a VirtualizingStackPanel behaves the same as an ordinary StackPanel.


如下所例,用IsVirtualizing这个attached属性,我们可以控制ListBox的虚拟化。

So an example of such, with the attached property, IsVirtualizing, you can Virtualizing on ListBox as such ..


      xmlns=""

      xmlns:x=""

      Title="ListboxVirutalizationPage"

      VerticalAlignment="Top"

      >

   

       

       

       

            x:Key="NameDataStyle"

            >

           

                Text="{Binding XPath=@name}"

                FontFamily="Arial"      

                FontSize="8"

                Foreground="Black"

                       >

           

       

   

   

   

   

        HorizontalAlignment="Left"

        VerticalAlignment="Top"

        BorderBrush="Black"

        BorderThickness="2">

       

            DataContext="{StaticResource Leagues}"

            >

           

                Text="{Binding XPath=@name}"

                FontFamily="Arial"

                FontSize="18"

                Foreground="Black"

                >

       

            VirtualizingStackPanel.IsVirtualizing="True"

            ItemsSource="{Binding XPath=Team}"

            ItemTemplate="{DynamicResource NameDataStyle}"

            >

       

   


可以进一步的控制VirtualizationMode,值Recycling和Standard。

You can further control the VirtualizationMode, possible value are Recycling and Standard.

      xmlns=""

      xmlns:x=""

      xmlns:src="clr-namespace:DemoVirtualizingStackPanel.ViewModels"

      Title="RecyclingVirtualizationPage">

 

  

      

          

      

      

           Height="150"

           ItemsSource="{StaticResource data}"

           VirtualizingStackPanel.VirtualizationMode="Recycling"

           >

  


如果你感兴趣的话,ViewModel定义如下:

For your interest, the ViewModel is like this:


        public class LotsOfItems : ObservableCollection

        {

             public LotsOfItems()

             {

                 for (int i = 0; i < 1000; i++)

                     this.Add("Item " + i.ToString());

             }

        }


Xml provider的数据xml如下:

And the Xml that provides the data is like this:


// Leagues.xml

 

   

   

   

   

   

   

   

   

   

   

 

ItemsPanelTemplate

在我们看到的第一个例子中,要介绍的第二个元素是“ItemsPanelTemplate”.


And in the first example that we introduced, the second items that we may use is the “ItemsPanelTemplate”.

ItemsPanelTemplate定义了作为布局元素的panel,GroupStyle有一个panel属性,他的类型是ItemsPanelTemplate,ItemsControl类型有一个ItemsPanel属性,值是ItemsPanelTemplate.每一个ItemsControl有一个默认的ItemsPanel类型,ItemsControl的ItemsPanel是StackPanel, ListBoxItemsPanel是VirtualizingStackPanel,MenuItem的值是WrapPanel,Status的为DockPanel.


The ItemsPanelTemplate specifies the panel that is used for the layout of items.  has a  property that is of type ItemsPanelTemplate. types have an  property that is of type ItemsPanelTemplate.

Each  type has a default ItemsPanelTemplate. For the  class, the default  value is an ItemsPanelTemplate that specifies a. For the , the default uses the . For , the default uses . For , the default uses.


下面的例子中,我们将要介绍几种创建vertical ListBox的方法。

In the following examples. We will introduce ways to create Vertical ListBox.


例子1 – ItemsPanel 属性


Example 1 – With the ItemsPanel property


       


装配起来,我们定义了“DataTemplate” and the “XmlDataProvider”,整体的代码如下:

To wire it up, we defined the “DataTemplate” and the “XmlDataProvider”. The overall code is as follow.


      xmlns=""

      xmlns:x=""

      Title="HorizontalListBoxPage">

   

       

       

            x:Key="NameDataStyle"

            >

           

                Text="{Binding XPath=@name}"

                FontFamily="Arial"      

                FontSize="8"

                Foreground="Black"

                       >

            

       

       

       

   

   

       

       

            DataContext="{StaticResource Leagues}"

            ItemsSource="{Binding XPath=Team}"

            ItemTemplate="{DynamicResource NameDataStyle}"

            >

   

 

在后面的例子中,我们将复用ListBox, XmlDataProvider 和DataTemplate定义。

 

We will reuse the ListBox, XmlDataProvider and DataTemplate definition in following examples.

 

例子2

Example 2

除了使用ItemsPanelTemplate,在下面的例子中,我们再ControlTemplate里定义了一个horizontal stackpanel,请注意,我们使用了IsItemsHost属性,表示生成的元素将到这个StackPanel里面去。

Instead of using the ItemsPanelTemplate, in this example, we specify a horizontal stackpanel within the ControlTemplate, note that we uses IsItemsHost property of the StackPanel, indicating that the generated items should go in the panel..

 

这个方法的不足是,我们不替换ControlTemplate就不能替换ItemsPanelTemplate。Style的定义如下

The drawback is user cannot replace the ItemsPanelTemplate without using a ControlTemplate. So using this technique with care.

Style definition is as follow.

   

 

例子3

Example 3:

我们可以同时替换ItemsPanel和Control Template,代码如下:

We can alternatively replace both the ItemsPanel and the Control Template, code as follow.

 

   

 

 

主函数如下

In general, the Main code is as such



        [STAThread]

        [DebuggerNonUserCode]

        public static void Main(string[] args)

        {

            App app = new App();

            //app.InitializeComponent();

            app.Run(new MainWindow());

        }


在主窗口的 代码种,加入如下的Page代码(ListBox写在Page里面).

And in the MainWindow, do things like this for the pages that contains the ListBox with Customizing Styles.


   

        VerticalAlignment="Stretch"

        HorizontalAlignment="Stretch"

        Height="300"

        Width="450"

        xmlns:page="clr-namespace:DemoItemsPanelTemplate"

        >


Or

   

        VerticalAlignment="Center"

        HorizontalAlignment="Center"

        Height="300"

        Width="450"

        xmlns:page="clr-namespace:DemoItemsPanelTemplate">

   

Or

   

        VerticalAlignment="Center"

        HorizontalAlignment="Center"

        Height="300"

        Width="450"

        xmlns:page="clr-namespace:DemoItemsPanelTemplate"

        >


引用:

References:

 

阅读(2093) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~