WPF Datagrid 分组和排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16050211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Datagrid group and sort
提问by Chubsdad
I am implementing grouping in WPF datagrid. I want to sort the grouped items. For example datagrid is having four columns(empno,name,dept,address). I am doing grouping by dept column. when I click on the dept column header I want to sort the grouped items.
我正在 WPF 数据网格中实现分组。我想对分组的项目进行排序。例如数据网格有四列(empno、name、dept、address)。我正在按部门列进行分组。当我单击部门列标题时,我想对分组的项目进行排序。
Here I am using ListCollectionView to group the items in the code behind.
这里我使用 ListCollectionView 对后面的代码中的项目进行分组。
public ListCollectionView collection;
collection = new ListCollectionView(obj.empData);
collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
dgData.Items.SortDescriptions.Add
(new System.ComponentModel.SortDescription
("Country"
,System.ComponentModel.ListSortDirection.Descending
)
);
dgData.Items.SortDescriptions.Add
(new System.ComponentModel.SortDescription
("Contact"
, System.ComponentModel.ListSortDirection.Descending
)
);
dgData.ItemsSource = collection;
private void dgData_Sorting
(object sender, Microsoft.Windows.Controls.DataGridSortingEventArgs e)
{
if (e.Column.SortDirection.ToString() == "Ascending")
{
//dgData.Items.SortDescriptions.Clear();
collection.Refresh();
collection = new ListCollectionView(obj.empData);
collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
dgData.Items.SortDescriptions.Add
( new System.ComponentModel.SortDescription
("Country"
, System.ComponentModel.ListSortDirection.Descending
)
);
dgData.ItemsSource = collection;
}
}
After changing the sort order it is not reflecting in the UI. Please let me know the correct way to implement this.
更改排序顺序后,它不会反映在 UI 中。请让我知道实现这一点的正确方法。
回答by Gennady Vanin Геннадий Ванин
Have you seen the MSDN article How to: Sort a GridView Column When a Header Is Clickedwhich refers to the sample from ListView That Sorts Data Sampleand the latter has (Download sample) link
您是否看过 MSDN 文章如何:单击标题时对 GridView 列进行排序,该文章引用了ListView 中对数据示例进行排序的示例,后者具有(下载示例)链接
Funny but the eventual reference to sample downloadis available only through .NET 3.0 and 3.5 version of the MSDN article but not through versions for .NET 4.0 and 4.5 though the code snippets are the same.
有趣的是,最终对示例下载的引用只能通过 MSDN 文章的 .NET 3.0 和 3.5 版本获得,但不能通过 .NET 4.0 和 4.5 版本获得,尽管代码片段是相同的。
There are also bog articles with samples based on above MSDN sample:
还有一些带有基于上述 MSDN 示例的示例的 bog 文章:
- Thomas Levesque's [WPF] Automatically sort a GridView when a column header is clicked
- Thomas Levesque's [WPF] Automatically sort a GridView (continued)
The latter also has a code
- Thomas Levesque 的 [WPF] 单击列标题时自动对 GridView 进行排序
- Thomas Levesque 的[WPF] Automatically sort a GridView (continued)
后者也有代码
There is also MSDN blog articles with runnable Visual Studio project (with depemnency on WPF Toolkit) :
还有带有可运行 Visual Studio 项目的 MSDN 博客文章(依赖于 WPF 工具包):
回答by mathieu
You can use this code (limitation : the "Country" order will reset to ascending when sorting on "Contact") :
您可以使用此代码(限制:按“联系人”排序时,“国家/地区”顺序将重置为升序):
void dgData_Sorting(object sender, DataGridSortingEventArgs e)
{
// reset sortings
collection.SortDescriptions.Clear();
// define column sort
e.Column.SortDirection = e.Column.SortDirection
== ListSortDirection.Ascending
? ListSortDirection.Descending : ListSortDirection.Ascending;
// sort collection
collection.SortDescriptions.Add
(new SortDescription
(e.Column.SortMemberPath
, e.Column.SortDirection.GetValueOrDefault()
)
);
// mark event as handled otherwise the datagrid will "reset" your custom sorting
e.Handled = true;
}
回答by philu
I found that turning on Live Sorting makes the second sort column actually take affect, e.g:
我发现打开实时排序会使第二个排序列实际生效,例如:
collection.IsLiveSortingRequested = true;
collection.LiveSortingProperties.Clear();
collection.LiveSortingProperties.Add("Contact");

