Tôi đã lấy một ví dụ từ MSDN documentation và điều chỉnh nó để nâng cao một sự kiện Sắp xếp khi sự kiện Sorting được thực hiện.
public class CustomDataGrid : DataGrid
{
// Create a custom routed event by first registering a RoutedEventID
// This event uses the bubbling routing strategy
public static readonly RoutedEvent SortedEvent = EventManager.RegisterRoutedEvent(
"Sorted", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CustomDataGrid));
// Provide CLR accessors for the event
public event RoutedEventHandler Sorted
{
add { AddHandler(SortedEvent, value); }
remove { RemoveHandler(SortedEvent, value); }
}
// This method raises the Sorted event
void RaiseSortedEvent()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomDataGrid.SortedEvent);
RaiseEvent(newEventArgs);
}
protected override void OnSorting(DataGridSortingEventArgs eventArgs)
{
base.OnSorting(eventArgs);
RaiseSortedEvent();
}
}
Sau đó, bạn có thể sử dụng tính năng này trong codebehind.
datagrid.Sorted += new RoutedEventHandler(datagrid_Sorted);
hoặc trong XAML
<local:CustomDataGrid x:Name="datagrid" Sorted="datagrid_Sorted;"/>
MSDN có thể là một nơi tốt hơn để hỏi "tại sao". –
Xử lý Sắp xếp và chỉ chuyển loại cho DataGrid. – Paparazzi
xử lý sắp xếp? Bạn có nghĩa là phân loại? – Houman