WPF Knowledge Points - 默认视图(DefaultView),CollectionSourceView,CollectionView的区别
2017-06-14 17:45
387 查看
这些天一直在做一些关于Treeview的事情,想写出来一些用法和心得。说到集合对象的显示和表现,CollectionSourceView和CollectionView有着至关重要的作用,所以在写TreeView之前,先把这两个类和一些相关概单独拎出来整理一下。
WPF的默认视图(DefaultView)
WPF中的数据绑定,如果直接绑定到一个集合(实现了IEnumerable的类),会有一个视图被隐式的插入到源集合对象和目标对象之间,这个视图就是和集合相关联的默认视图。视图(实现了ICollectionView接口的对象)存储着集合当前项信息,也用于支持排序,分组,过滤和导航。事实上,所有的集合绑定都是目标对象绑定到集合的视图上,而不是直接绑定到集合本身。
我们看一下MSDN里面的标准描述要点:
1. In WPF applications, all collections have an associated default collection view. Rather than working with the collection directly, the binding engine always accesses the collection through the associated view.
2. An internal class based on CollectionView is the default view for collections that implement only IEnumerable. ListCollectionView is the default view for collections that implement IList. BindingListCollectionView is the default view for collections that implement IBindingListView or IBindingList.
我们可以用一下代码得到一个集合的默认视图:
注意:出于性能的考虑,默认视图会按需创建,也就是说一个集合可以拥有一个默认视图,但是如果这个集合没有被尝试过任何直接数据绑定的情况下,是不会创建默认视图的。
CollectionView
上面的描述让我们知道了默认视图的概念和作用场合。接下来的问题就来了,WPF可以隐式的创建默认视图,既然有默认视图,那应该也有对应的自定义视图,我们应该怎么建立和使用?这个就是我们马上要讨论的问题。
老规矩,我摘取了一下MSDN中的要点给大家:
1. Represents a view for grouping, sorting, filtering, and navigating a data collection.
2. You can think of a collection view as a layer on top of a binding source collection that allows you to navigate and display the collection based on sort, filter, and group queries, all without having to manipulate the underlying source collection itself.If the source collection implements the INotifyCollectionChanged interface, the changes that raise the CollectionChanged event are propagated to the views.
3. Because a view does not change the underlying source collection, a source collection can have multiple views associated with it.By using views, you can display the same data in different ways.For example, you can use two views on a collection of Task objects to show tasks sorted by priority on one part of the page and grouped by area on another part of the page.
总结出来,CollectionView就是一个视图类,它的实例代表了一个视图类实例,视图可以用来做集合对象显示的相关操作(分组,排序,过滤,导航等)。视图的优点有:对视图的分组,过滤等等操作不会影响到源集合本身;一个集合可以有多个不同的视图,源集合的改变有能力通知到它所有的视图(前提是源集合实现了INotifyCollectionChanged接口)。
很显然,默认视图DefaultView就是CollectionView的一个实例。虽然用ICollectionView引用视图更常用,但是我们也可以:
CollectionViewSource
验明了视图的真身,我们在看看我们更常用的“真身”的辅助代理。
依然先MSDN要点:
1. CollectionViewSource is a proxy for a CollectionView class, or a class derived from CollectionView.
2. CollectionViewSource enables XAML code to set the commonly used CollectionView properties, passing these settings to the underlying view.
3. CollectionViewSource has a View property that holds the actual view and a Source property that holds the source collection.
有了前面的铺垫,CollectionViewSource就好理解了,它是CollectionView类针对XAML的一个代理类。对CollectionViewSource的操作和对其XAML的设置最终会被应用到它的CollectionView上。
创建和使用自定义CollectionView
我们要创建一个集合(IEnumerable)的视图,我们可以先创建一个CollectionViewSource的实例A,把集合赋给该实例的Source属性,然后我们在该实例的View属性就可以得到我们需要的一个视图了。
C#例子:
XAML例子(我们可以看到对过滤,分组,排序的XAML支持):
理解好CollectionView,CollectionViewSource对于集合数据绑定操作非常重要。关于分组,排序等功能的详细使用,我会在稍后TreeView关于非树形集合视图分组显示映射到树形多级结构的文章中配合其他知识点讲解说明。
转载:http://www.cnblogs.com/KeithWang/archive/2011/11/24/2259282.html
WPF的默认视图(DefaultView)
WPF中的数据绑定,如果直接绑定到一个集合(实现了IEnumerable的类),会有一个视图被隐式的插入到源集合对象和目标对象之间,这个视图就是和集合相关联的默认视图。视图(实现了ICollectionView接口的对象)存储着集合当前项信息,也用于支持排序,分组,过滤和导航。事实上,所有的集合绑定都是目标对象绑定到集合的视图上,而不是直接绑定到集合本身。
我们看一下MSDN里面的标准描述要点:
1. In WPF applications, all collections have an associated default collection view. Rather than working with the collection directly, the binding engine always accesses the collection through the associated view.
2. An internal class based on CollectionView is the default view for collections that implement only IEnumerable. ListCollectionView is the default view for collections that implement IList. BindingListCollectionView is the default view for collections that implement IBindingListView or IBindingList.
我们可以用一下代码得到一个集合的默认视图:
ICollectionView defaultView = CollectionViewSource.GetDefaultView(myCollection);
注意:出于性能的考虑,默认视图会按需创建,也就是说一个集合可以拥有一个默认视图,但是如果这个集合没有被尝试过任何直接数据绑定的情况下,是不会创建默认视图的。
CollectionView
上面的描述让我们知道了默认视图的概念和作用场合。接下来的问题就来了,WPF可以隐式的创建默认视图,既然有默认视图,那应该也有对应的自定义视图,我们应该怎么建立和使用?这个就是我们马上要讨论的问题。
老规矩,我摘取了一下MSDN中的要点给大家:
1. Represents a view for grouping, sorting, filtering, and navigating a data collection.
2. You can think of a collection view as a layer on top of a binding source collection that allows you to navigate and display the collection based on sort, filter, and group queries, all without having to manipulate the underlying source collection itself.If the source collection implements the INotifyCollectionChanged interface, the changes that raise the CollectionChanged event are propagated to the views.
3. Because a view does not change the underlying source collection, a source collection can have multiple views associated with it.By using views, you can display the same data in different ways.For example, you can use two views on a collection of Task objects to show tasks sorted by priority on one part of the page and grouped by area on another part of the page.
总结出来,CollectionView就是一个视图类,它的实例代表了一个视图类实例,视图可以用来做集合对象显示的相关操作(分组,排序,过滤,导航等)。视图的优点有:对视图的分组,过滤等等操作不会影响到源集合本身;一个集合可以有多个不同的视图,源集合的改变有能力通知到它所有的视图(前提是源集合实现了INotifyCollectionChanged接口)。
很显然,默认视图DefaultView就是CollectionView的一个实例。虽然用ICollectionView引用视图更常用,但是我们也可以:
CollectionView defaultView = CollectionViewSource.GetDefaultView(myCollection) as CollectionView;
CollectionViewSource
验明了视图的真身,我们在看看我们更常用的“真身”的辅助代理。
依然先MSDN要点:
1. CollectionViewSource is a proxy for a CollectionView class, or a class derived from CollectionView.
2. CollectionViewSource enables XAML code to set the commonly used CollectionView properties, passing these settings to the underlying view.
3. CollectionViewSource has a View property that holds the actual view and a Source property that holds the source collection.
有了前面的铺垫,CollectionViewSource就好理解了,它是CollectionView类针对XAML的一个代理类。对CollectionViewSource的操作和对其XAML的设置最终会被应用到它的CollectionView上。
创建和使用自定义CollectionView
我们要创建一个集合(IEnumerable)的视图,我们可以先创建一个CollectionViewSource的实例A,把集合赋给该实例的Source属性,然后我们在该实例的View属性就可以得到我们需要的一个视图了。
C#例子:
CollectionViewSource viewSource = new CollectionViewSource(); viewSource.Source = myCollection; ICollectionView view = viewSource.View;
XAML例子(我们可以看到对过滤,分组,排序的XAML支持):
<CollectionViewSource x:Key="myViewSource" Source="{Binding Source={StaticResource MyCollection}}" Filter="CollectionViewSourceFilterEvent"> <CollectionViewSource.SortDescriptions> <SortDescription PropertyName="City"/> </CollectionViewSource.SortDescriptions> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="Area"/> </CollectionViewSource.GroupDescriptions> </CollectionViewSource>
理解好CollectionView,CollectionViewSource对于集合数据绑定操作非常重要。关于分组,排序等功能的详细使用,我会在稍后TreeView关于非树形集合视图分组显示映射到树形多级结构的文章中配合其他知识点讲解说明。
转载:http://www.cnblogs.com/KeithWang/archive/2011/11/24/2259282.html
相关文章推荐
- WPF Knowledge Points - 默认视图(DefaultView),CollectionSourceView,CollectionView的区别
- WPF内存泄露:CollectionViewSource.GetDefaultView导致Cache对象
- WPF内存泄露:CollectionViewSource.GetDefaultView导致Cache对象
- CollectionViewSource ObservableCollection 的比较 (CollectionView 与 CollectionViewSource PagedCollectionView )
- wpf CollectionViewSource的运用
- WPF CollectionViewSource 的排序
- WPF CollectionViewSource CollectionView
- Wpf ViewModel中 ObservableCollection不支持从调度程序线程以外的线程对其 SourceCollection 进行的更改
- wpf,CollectionViewSource,使用数据过滤 筛选 功能。
- WPF Knowledge Points - ControlTemplate和DataTemplate的区别
- WPF中的CollectionView与CollectionViewSource
- wpf CollectionViewSource与ListBox的折叠、分组显示,及输入关键字 Filter的筛选
- 谈谈WPF中的CollectionView与CollectionViewSource (1)
- WPF使用BindingListCollectionView 和 ListCollectionView 视图排序集合
- 谈谈WPF中的CollectionView与CollectionViewSource (1)
- WPF Knowledge Points - ContentControl和ContentPresenter的区别
- WPF Knowledge Points - TemplateBinding和Binding的区别
- 谈谈WPF中的CollectionView与CollectionViewSource (1)
- .NET中的视图和过滤器 (DefaultView和RowFilter)
- ip default-network和ip route 0.0.0.0 0.0.0.0默认路由的区别