如果 DataGrid 列已排序(而不是排序),我如何收到通知 [英] How can I be notified if a DataGrid column is sorted (and not sorting)

查看:16
本文介绍了如果 DataGrid 列已排序(而不是排序),我如何收到通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为 DataGrid 在 WPF 应用程序中,但找不到获取它的方法.

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 提供了一个事件 Sorting,但我不能使用它,因为它在排序完成之前被触发.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?

为了更清楚地说明我想要实现的目标:当用户对列进行排序时,我需要知道哪个 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 ;)

推荐答案

我自己通过重写 DataGrid 为 DataGrid 事件实现了 Sorted,如下所示:

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屋!

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