Существует обходной путь для предоставления перенаправленного события, когда у вас есть только «нормальное» событие:
Создайте вложенное свойство, которое управляет переадресацией события, и вложенное событие, которое должно заменить исходное событие.Для этого создайте класс DataGridEx
(какое бы имя класса вы ни выбрали) в качестве контейнера для присоединенного свойства (DataGridEx.EnableSortingEvent
) и события (DataGridEx.Sorting
).
Также создайте пользовательскийRoutedEventArgs
класс, который пересылает исходные аргументы события сортировки
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
- это xmlns для пространства имен, в котором определен DataGridEx
):
<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
}
Надеюсь, это то, что вам нужно.Пришлось освежить мою память о прикрепленном материале:)