且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2021-11-29 01:22:14

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://***.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://***.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