ICollectionView 接口类型的变量
本文关键字:变量 接口类型 ICollectionView | 更新日期: 2023-09-27 18:29:43
我是WPF的新手,看起来我也不完全了解C#。
下面的代码应该向 DataGrid 提供排序数据。
这是我难以理解的代码:
ObservableCollection<Person> PersonsCollection = new ObservableCollection<Person>();
//this one is easy: I create new collection for objects of class Person and I call it PersonsCollection
ICollectionView PersonsView = CollectionViewSource.GetDefaultView(PersonsCollection);
//this one is more complicated. Does it mean that I create new object called PersonsView which I assume that implements ICollectionView interface?
ListCollectionView personsView = PersonsView as ListCollectionView;
//this one I do not understand. Why do we need it? How can we treat PersonsView as ListCollectionView?
personsView.CustomSort = new PersonSorter();
//here we assign object of PersonSorter class to do the sorting. Fine with me.
dataGrid1.ItemsSource = personsView;
//and here we assign personsView as a ItemsSource for our DataGrid. Fine with me.
有什么帮助吗?谢谢 :-(
这个很简单:我为类 Person 的对象创建新集合,我称之为 PersonsCollection。
正确,但我想先清除一些事情。您可以在此处使用任何集合,或者更准确地说,可以使用任何IEnumberable
。
ObservableCollection
与普通IEnumerable
的区别在于,当在集合中添加、删除或重新排序项目时,前者会引发通知,而后者则不会。
重要说明:需要注意的一点是,无论哪种类型的集合,无论是IEnumberable
还是ObservableCollection
,当该集合用于绑定时,WPF 系统会围绕该集合(源(创建一个包装器,有点像默认视图。
该视图实现了ICollectionView
.它保留当前项目的概念,并提供排序,导航,过滤和分组等功能。
此视图与集合(源(相关,因此,如果对同一集合有多个绑定,则所有这些绑定实际上都绑定到 WPF 系统创建的默认视图,以便在更新默认视图时一起更新它们。
我必须澄清最后一个重要议题,因为它关系到前面的问题。
这个更复杂。这是否意味着我创建了名为PersonsView的新对象,我假设该对象实现了
ICollectionView
接口?
没有或至少不完全正确。您正在获取对将由 WPF 系统创建的默认视图的引用,这就是为什么获取该对象的方法称为 GetDefaultView()
而不是类似 CreateDefaultView()
。
这个我不明白。我们为什么需要它?我们如何将 PersonsView 视为 ListCollectionView?
我们真的不需要它,我们可以没有这条线。我们可以将PersonView
视为ListCollectionView
,因为
所有集合都有一个默认的集合视图。对于所有集合 实现 IList,ListCollectionView 对象是默认视图 对象。
MSDN 文档。
其余的都是你的罚款和我的罚款,所以没有必要评论。
这是我的理解:
您的第一行:
ObservableCollection<Person> PersonsCollection = new ObservableCollection<Person>();
正如您正确所说,正在创建可观察的Persons
集合,您可以直接绑定到此集合(从技术上讲,我相信您绑定到默认CollectionView
(,并且您的DataGrid
将收到有关集合更改的通知,并相应地更新。但是,您需要对数据进行排序的附加功能。
所以,你的第二行:
ICollectionView PersonsView = CollectionViewSource.GetDefaultView(PersonsCollection);
使用 GetDefaultView
返回PersonsCollection
的默认CollectionView
,CollectionView
是现有集合的包装器,它将为您提供其他行为,例如筛选、分组、导航和排序。在 WPF 中绑定集合时,绑定到CollectionView
对象,在对数据进行排序和显示时,正是这些视图作,等等。
巧是这种情况,这种类型的默认CollectionViewSource
类型是ListCollectionView
,但由于这可能会因您传递给CollectionViewSource.GetDefaultView()
的对象类型而异,因此该方法返回一个接口,ICollectionView
。
所以在这一点上,我们可以使用ICollectionView
,PersonsView
作为ItemsSource
,但我们希望通过定义一个可以在ListCollectionView
上找到的CustomSort
来定义自定义排序行为。
由于我们知道默认视图是ListCollectionView
,我们可以相应地显式转换ICollectionView
对象(第 3 行(,然后设置所需的排序行为(第 4 行(:
ListCollectionView personsView = PersonsView as ListCollectionView;
personsView.CustomSort = new PersonSorter();
希望一些知识渊博的用户会指出我犯的任何错误。
您正在创建一个ObservableCollection,该集合用于在更改DP值时触发CollectionChanged事件。
ICollectionView 您正在从人员集合创建视图,该视图将用于在 WPF 数据网格等中显示集合数据。
ListCollectionview对ICollectionView微笑,但您可以对itmes等进行排序或过滤。
personsView.CustomSort = new PersonSorter(); // depending on which Tech you are using for example it might be you are creating the sorting property Name.