如果DataGrid列被排序(而不是排序),我如何被通知 [英] How can I be notified if a DataGrid column is sorted (and not sorting)
问题描述
我需要为排序事件。一个WPF应用程序中的aspxrel =noreferrer> DataGrid
,但找不到方法。
I need to have kind of an Sorted
event for a DataGrid
in a WPF application but cannot find a way to get it.
这是我试过的:
DataGrid
提供一个事件 排序
,但是我不能使用它,因为它在排序完成之前被触发。 EventArgs
给我列出排序的列,而不是排序的方式,如果我将排序方向设置为旧值。当然,我可以猜测它会是什么,因为我知道它从没有升到升序,最后还是下降,但是如果控件的行为发生变化,那将不会解决。
The DataGrid
provides an event Sorting
, but I cannot use it as it is fired before the sorting is done. The EventArgs
give me the column which is sorted but not the way it is sorted and if I get the sort direction it is set to the old value. Of course I could guess what it will be as I know that it flips from none to ascending and finally to descending but that would be no solution as it would fail if the behavior of the control changes.
第二次尝试:
DataGrid
具有默认视图,可以访问 SortDescriptionCollection
。这个集合包含所有的排序属性,但是我看不到有任何可能的通知我的更改。
The DataGrid
has a default view which provides access to a SortDescriptionCollection
. This collection holds all sorting properties but I don't see any possibility to let me inform about changes.
我不得不说,我正在寻找一个清洁的解决方案尽可能的,因为它将被用于一个大型项目,在这个大型项目中我无法使用可能会导致环境变化的解决方案。
I have to say that I'm looking for a solution as clean as possible as it will be used in a large project on which I can't use solutions which could fail if the environment changes.
有没有人从经验(或文档) ?)我如何解决这个问题?
Does anyone know from experience (or documentation?) how I could solve this problem?
编辑:要更清楚我想要实现的目标: code> DataGrid 列按用户排序列的方向进行排序。这个信息没有必要在排序本身之后,它只是必须是正确的;)
To make more clear what I want to achieve: I need to get informed which DataGrid
column is sorted in which direction when a user sort a column. It is not necessary that this information comes after the sorting itself, it just has to be correct ;)
推荐答案
我实现了Sorted对于DataGrid事件,我自己通过覆盖DataGrid如下:
I implemented the Sorted for the DataGrid event myself by overriding the DataGrid as follows:
public class ValueEventArgs<T> : EventArgs
{
public ValueEventArgs(T value)
{
Value = value;
}
public T Value { get; set; }
}
public class DataGridExt : DataGrid
{
public event EventHandler<ValueEventArgs<DataGridColumn>> Sorted;
protected override void OnSorting(DataGridSortingEventArgs eventArgs)
{
base.OnSorting(eventArgs);
if (Sorted == null) return;
var column = eventArgs.Column;
Sorted(this, new ValueEventArgs<DataGridColumn>(column));
}
}
为了使用它,所有你需要做的是这样的:
In order to use it then all you need to do is this:
private void Initialize()
{
myGrid.Sorted += OnSorted;
}
private void OnSorted(object sender, ValueEventArgs<DataGridColumn> valueEventArgs)
{
// Persist Sort...
}
这篇关于如果DataGrid列被排序(而不是排序),我如何被通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!