如何从样式为WPF DataGrid应用自定义排序?

时间:2019-04-08 17:36:55

标签: wpf sorting datagrid

我有一种要应用于DataGrid的样式。 DataGrid需要运行我的自定义排序代码,而不是默认的DataGrid排序。我尝试的解决方案如下:

<Style TargetType="{x:Type DataGrid}" x:Key="FilteredDataGrid">
    <EventSetter Event="Sorting" Handler="DataGrid_Sorting"/>
</Style>

在后面的代码中:

private void DataGrid_Sorting(object sender, DataGridSortingEventArgs e) {

    e.Handled = true;

    //Here is where I put the code for my custom sort.

}//DataGrid_Sorting

但是,此代码无法构建。在我看来,由于DataGrid.Sorting事件不是RoutedEvent,因此无法在EventSetter中使用。

如何为应用了样式的任何DataGrid自定义排序?

1 个答案:

答案 0 :(得分:1)

有一种变通方法,当您只有一个“正常”事件时提供路由事件:

创建一个控制事件转发的附加属性和一个将替换原始事件的附加事件。为此,请创建一个类DataGridEx(您喜欢的任何类名称)作为附加属性(DataGridEx.EnableSortingEvent)和事件(DataGridEx.Sorting)的容器。

此外,创建一个自定义RoutedEventArgs类来转发原始排序事件args

public class DataGridExSortingEventArgs : RoutedEventArgs
{
    public DataGridExSortingEventArgs(RoutedEvent routedEvent, DataGridSortingEventArgs sourceEventArgs) : base(routedEvent)
    {
        SourceEventArgs = sourceEventArgs;
    }

    public DataGridSortingEventArgs SourceEventArgs { get; set; }
}

public static class DataGridEx
{
    public static bool GetEnableSortingEvent(DependencyObject obj)
    {
        return (bool)obj.GetValue(EnableSortingEventProperty);
    }

    public static void SetEnableSortingEvent(DependencyObject obj, bool value)
    {
        obj.SetValue(EnableSortingEventProperty, value);
    }

    // Setting this property to true enables the event forwarding from the DataGrid.Sorting event to the DataGridEx.Sorting RoutedEvent
    public static readonly DependencyProperty EnableSortingEventProperty = DependencyProperty.RegisterAttached(
        "EnableSortingEvent",
        typeof(bool),
        typeof(DataGridEx),
        new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEnableSortingChanged)));

    private static void OnEnableSortingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is DataGrid dg)
        {
            if ((bool)e.NewValue)
                dg.Sorting += Dg_Sorting;
            else
                dg.Sorting -= Dg_Sorting;
        }
    }

    // When DataGrid.Sorting is called and DataGridEx.EnableSortingEvent is true, raise the DataGridEx.Sorting event
    private static void Dg_Sorting(object sender, DataGridSortingEventArgs e)
    {
        if (sender is DataGrid dg && GetEnableSortingEvent(dg))
        {
            dg.RaiseEvent(new DataGridExSortingEventArgs(SortingEvent, e));
        }
    }

    // When DataGridEx.EnableSortingEvent is true, the DataGrid.Sorting event will be forwarded to this routed event
    public static readonly RoutedEvent SortingEvent = EventManager.RegisterRoutedEvent(
        "Sorting",
        // only effective on the DataGrid itself
        RoutingStrategy.Direct,
        typeof(RoutedEventHandler),
        typeof(DataGridEx));

    public static void AddSortingHandler(DependencyObject d, RoutedEventHandler handler)
    {
        if (d is DataGrid dg)
            dg.AddHandler(SortingEvent, handler);
    }

    public static void RemoveSortingHandler(DependencyObject d, RoutedEventHandler handler)
    {
        if (d is DataGrid dg)
            dg.RemoveHandler(SortingEvent, handler);
    }
}

现在使用您所使用的样式(local是定义DataGridEx的命名空间的xmlns)

<Style TargetType="DataGrid">
    <Setter Property="local:DataGridEx.EnableSortingEvent" Value="True"/>
    <EventSetter Event="local:DataGridEx.Sorting" Handler="DataGrid_Sorting"/>
</Style>

处理程序

private void DataGrid_Sorting(object sender, RoutedEventArgs e)
{
    if (e is DataGridExSortingEventArgs args)
    {
        // will prevent datagrid default sorting
        args.SourceEventArgs.Handled = true;
    }

    // More stuff
}

我希望这是您所需要的。不得不刷新我对附件的记忆:)