如何将自定义排序规则应用于 WPF DataGrid?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2129601/
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
How can I apply a custom sort rule to a WPF DataGrid?
提问by devuxer
When the user does a column sort in my DataGrid
, I want all null or empty cells to be sorted to the bottom, rather than the top.
当用户在 my 中进行列排序时DataGrid
,我希望将所有空单元格或空单元格排序到底部,而不是顶部。
I wrote an IComparer<T>
that makes sure blanks are always sorted downward, but I can't figure out how to apply it to the columns of my DataGrid
. Note that the initialsort of the DataGrid
, which I'm doing with the LINQ OrderBy()
method, works great. The problem is that all subsequent sorts performed by the user sort the blanks to the top.
我写了一个IComparer<T>
确保空白总是向下排序的,但我不知道如何将它应用于我的DataGrid
. 请注意,我使用 LINQ方法对 的初始排序效果很好。问题是用户执行的所有后续排序都将空白排序到顶部。DataGrid
OrderBy()
Comparer Code
比较器代码
public class BlankLastStringComparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (string.IsNullOrEmpty(x) && !string.IsNullOrEmpty(y))
return 1;
else if (!string.IsNullOrEmpty(x) && string.IsNullOrEmpty(y))
return -1;
else
return string.Compare(x, y);
}
}
Questions
问题
How do I get the DataGridColumn
to use my comparer? Or if this is not possible, can you offer a workaround? I'm hoping for an MVVM friendly solution if possible.
我怎样DataGridColumn
才能使用我的比较器?或者,如果这是不可能的,您能否提供解决方法?如果可能,我希望有一个 MVVM 友好的解决方案。
回答by Aran Mulholland
this is how i do it : I do derive from the grid to keep all of this inside the class, so i attach to event handlers internally
这就是我的做法:我确实从网格派生以将所有这些保留在类中,因此我在内部附加到事件处理程序
attach to the sorting event
附加到排序事件
dataGrid.Sorting += new DataGridSortingEventHandler(SortHandler);
implement the method (i do this in a derived class)
实现该方法(我在派生类中执行此操作)
void SortHandler(object sender, DataGridSortingEventArgs e)
{
DataGridColumn column = e.Column;
IComparer comparer = null;
//i do some custom checking based on column to get the right comparer
//i have different comparers for different columns. I also handle the sort direction
//in my comparer
// prevent the built-in sort from sorting
e.Handled = true;
ListSortDirection direction = (column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;
//set the sort order on the column
column.SortDirection = direction;
//use a ListCollectionView to do the sort.
ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(this.ItemsSource);
//this is my custom sorter it just derives from IComparer and has a few properties
//you could just apply the comparer but i needed to do a few extra bits and pieces
comparer = new ResultSort(direction);
//apply the sort
lcv.CustomSort = comparer;
}
回答by trilson86
I have an MVVM solution for this problem which makes use of attached behaviours. If you prefer to use code-behind, @Aran's solution will do the trick too.
对于这个问题,我有一个 MVVM 解决方案,它利用了附加的行为。如果您更喜欢使用代码隐藏,@Aran 的解决方案也可以解决问题。