WPF Knowledge Points - 默认视图(DefaultView),CollectionSourceView,CollectionView的区别
这些天一直在做一些关于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.
我们可以用一下代码得到一个集合的默认视图:
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的区别的更多相关文章
- jQuery EasyUI Datagrid组件默认视图分析
在Datagrid基础DOM结构的一文中,我对Datagrid组件的骨架做了很详细的描述.有了骨架还并不完整,还得有血有肉有衣服穿才行.强大的Datagrid组件允许我们自己定义如何在基础骨架上长出健 ...
- 网页导出成word文档的默认视图方式问题
网页导出成word文档的默认视图方式问题 一般保存后的word文档默认是“Web版式视图”打开,这样会给客户的感觉不是真正的word文档,必须实现打开就是“页面视图” 1. 修改<html> ...
- 【WPF】SnapsToDevicePixels与UseLayoutRounding二者到底有什么区别?供参考
原文:[WPF]SnapsToDevicePixels与UseLayoutRounding二者到底有什么区别?供参考 MSDN上解释了一大堆,二者对比来看,并不能发现什么明显的区别,微软爸爸也不知道多 ...
- (27)odoo 中改变菜单动作的默认视图
一个动作下面有多个视图来支持,像表单视图.列表视图.看板视图.图表视图等 这时我们想改变系统默认指定的视图,方法其实有两种,一种是通过面板改,一种是开发一个小模块 举一例:项目默认打开是用了看板视图, ...
- wpf ComboBox设置默认值
最新的wpf的ComboBox设置默认值得方法是,给VM中的数据集合第一个元素插入一个提示项目,比如:请选择一项,然后通过数据绑定可以实现默认选中第一项,下面我就贴一下示例代码: xaml页面: &l ...
- wpf 自定义属性的默认值
public int MaxSelectCount { get { return (int)GetValue(MaxSelectCountProperty); } set { SetValue(Max ...
- Win7如何修改文件夹的默认视图,如何把详细信息改为平铺视图
先任意进入一个文件夹,右击选择平铺视图. 然后点击左上角的组织,文件夹和搜索选项,在文件夹选项的查看中点击"应用到文件夹",然后点击确定,弹出对话框,再确定. 随后再浏览别 ...
- WPF combobox设置默认选项不生效的问题
combobox 是常用的控件,当我们需要绑定设置默认选项时,往往会绑定 SelectedItem 属性去设置, 可是你会惊奇地发现SelectedItem的值绑定了, 它依旧是熟悉的模样 根据官方的 ...
- 在WPF按钮删除默认的鼠标悬停效果
如果你想在应用程序的所有按钮将此风格,那么这种风格可以插入Application.Resources部分的App.xaml页面. <Window.Resources> <Style ...
随机推荐
- 2019-11-29-Roslyn-通过-Nuget-管理公司配置
title author date CreateTime categories Roslyn 通过 Nuget 管理公司配置 lindexi 2019-11-29 08:58:52 +0800 201 ...
- PHP的htmlspecialchars、strip_tags、addslashes
PHP的htmlspecialchars.strip_tags.addslashes是网页程序开发中常见的函数,今天就来详细讲述这些函数的用法: 1.函数strip_tags:去掉 HTML 及 PH ...
- 如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权。否则,请按“取消(C)”按钮以防损坏
---恢复内容开始--- 解决办法:打开放此台Vmware虚拟机虚拟磁盘文件及配置文件存放的位置(也就是弹出提示窗口上的路径),删除后缀为.lck的文件夹 ---恢复内容结束---
- java map 根据 map的value值进行排序
//根据销量排行查询 public void queryGoodsByHotCount(){ //将map集合键和值封装到entry对象中 然后转换成set集合 Set<Entry<Int ...
- null 的应用
它是 Oracle 中非常特殊的一种类型.它表示不确定,表示没有值.并且它能转化成所有的类型. 向数据库中插入空字符串时,oracle 会把它自动转化为 null 类型.所以,在查询空字符的时候: s ...
- git提示Please enter a commit message to explain why this merge is necessary
Please enter a commit message to explain why this merge is necessary. 请输入提交消息来解释为什么这种合并是必要的(提交信息) gi ...
- electron-vue 图片加载失败后使用默认头像
<img :src="item.headUrl" alt="" class="contact-head" :onerror=" ...
- 【leetcode】1200. Minimum Absolute Difference
题目如下: Given an array of distinct integers arr, find all pairs of elements with the minimum absolute ...
- 【leetcode】1172. Dinner Plate Stacks
题目如下: You have an infinite number of stacks arranged in a row and numbered (left to right) from 0, e ...
- DNS预读取 dns-prefetch 提升页面载入速度
DNS Prefetch,即DNS预获取,是前端优化的一部分.一般来说,在前端优化中与 DNS 有关的有两点: 一个是减少DNS的请求次数,另一个就是进行DNS预获取 . DNS 作为互联网的基础协议 ...