我是新来的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.有什么帮助吗?谢谢:-)
发布于 2013-08-31 22:25:10
这一点很简单:我为类Person的对象创建了新的集合,并将其命名为PersonsCollection。
没错,但我想先澄清几件事。您可以在这里使用任何集合,或者更准确地说,可以使用任何IEnumberable。
ObservableCollection与普通IEnumerable的不同之处在于,当集合中添加、删除或重新排序项时,第一个IEnumerable会发出通知,而后者则不会。
重要:需要注意的一点是,无论是哪种类型的集合,无论是IEnumberable还是ObservableCollection,当绑定中使用该集合时,WPF系统都会围绕该集合(源)创建一个包装器,类似于默认视图。
该视图实现了ICollectionView。它保留当前项的概念,并提供排序、导航、筛选和分组等功能。
此视图与集合(源)相关,因此,如果对同一集合有多个绑定,那么所有这些绑定实际上都绑定到由WPF系统创建的默认视图,以便在更新默认视图时将它们一起更新。
我必须澄清最后一个重要议题,因为它涉及前面的问题。
这个更复杂。这是否意味着我创建了名为PersonsView的新对象,并假设它实现了
ICollectionView接口?
不,或者至少不是完全正确。您将得到对将由WPF系统创建的默认视图的引用,这就是为什么获取该对象的方法称为GetDefaultView(),而不是类似于CreateDefaultView()的方法。
这个我不明白。我们为什么需要它?我们如何将PersonsView视为ListCollectionView?
我们不需要它,不用这条线就行了。我们可以将PersonView视为ListCollectionView,因为
所有集合都有一个默认的CollectionView。对于所有实现IList的集合,ListCollectionView对象是默认的视图对象。
MSDN文档。
剩下的是你的罚款和我的罚款,所以不需要评论。
发布于 2013-08-31 22:19:36
我的理解是:
你的第一句话:
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,但是我们希望通过定义CustomSort来定义自定义排序行为,这可以在ListCollectionView上找到。
由于我们知道默认视图是ListCollectionView,所以我们可以相应地显式地转换ICollectionView对象(第3行),然后设置所需的排序行为(第4行):
ListCollectionView personsView = PersonsView as ListCollectionView;
personsView.CustomSort = new PersonSorter();希望一些更有见识的用户能指出我犯过的任何错误。
发布于 2013-08-31 21:27:35
ObservableCollection --您正在创建一个ObservableCollection,用于触发CollectionChanged事件,即更改DP值。
ICollectionView,您正在从person集合中创建视图,该视图将用于在WPF、datagrid等中显示集合数据。
ListCollectionview向ICollectionView微笑,但您可以对项目进行排序或筛选,等等。
personsView.CustomSort = new PersonSorter(); // depending on which Tech you are using for example it might be you are creating the sorting property Name.https://stackoverflow.com/questions/18552860
复制相似问题