如何在基于MVVM的WPF DataGrid中实现排序 [英] How to implement Sorting in WPF DataGrid based on MVVM

查看:818
本文介绍了如何在基于MVVM的WPF DataGrid中实现排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在基于MVVM模式的WPF DataGrid中实现排序?

How to implement Sorting in WPF DataGrid based on MVVM pattern?

DataGrid包含以下列:
1.软件
2.版本
3. TypeOfSoftware
4. ActivatedOn
5.状态
6.操作[激活,停用等]

DataGrid Contains the following columns:
1. Software
2. Version
3. TypeOfSoftware
4. ActivatedOn
5. Status
6. Actions [Activate, Deactivate etc]

要求:
1.当主要(TypeOfSoftware,ActivatedOn,Status)列中的值重复时,请使用"软件"中的值 列来确定二级排序顺序.如果该列也有重复的值,请使用"版本" 列为第三级分类.

Requirement:
1. When the Primary(TypeOfSoftware, ActivatedOn, Status) sorted column has duplicated values, use the values in the 'Software' column to determine the secondary sort order. When that column also has duplicated values, use the 'Version' column for the tertiary sort oder.

推荐答案

DataGrid具有内置的排序功能,因此您无需执行任何操作.它仅对视图进行排序,即,视图模型中的源集合未由DataGrid进行排序.视图模型并不真正在乎用户当前的查看方式 数据.

The DataGrid has built-in sorting functionality so you don't have to anything. It sorts the view only, i.e. the source collection in the view model is not sorted by the DataGrid. The view model doesn't really care about how the user is currently viewing the data.

实际上,每当您绑定到诸如ObservableCollection< T>之类的数据集合时,或List< T>在WPF中,您始终绑定到自动生成的视图,而不绑定到实际的集合本身.这样可以操纵视图 排序,分组,过滤等,而不会影响视图模型中的实际源集合.

In fact, whenever you bind to a collection of data such as an ObservableCollection<T> or a List<T> in WPF you are always binding to an automatically generated view and not to the actual collection itself. This way the view can be manipulated, sorted, grouped, filtered, etc., without affecting the actual source collection in the view model.

您还可以公开一个ICollectionView,从视图模型向其中添加SortDescriptions并将其绑定到该ICollectionView.请参考以下链接以获取更多有关此方面的信息:
http://stackoverflow.com/questions/18475883/sort-observablecollection-bound- to-datagrid-in-mvvm
http://www.wpftutorial.net/dataviews.html

You could also expose an ICollectionView to which you add SortDescriptions from your view model and bind to this one. Please refer to the following links for more informatio about this:
http://stackoverflow.com/questions/18475883/sort-observablecollection-bound-to-datagrid-in-mvvm
http://www.wpftutorial.net/dataviews.html

希望有帮助.

请记住,通过将有用的帖子标记为答案来关闭话题,然后在遇到新问题时开始新话题.请不要在同一线程中问几个问题

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread


这篇关于如何在基于MVVM的WPF DataGrid中实现排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆