//event handler for the GridViewColumnHeader click private static void OnColumnHeaderClicked(object sender, RoutedEventArgs e) { ListView view = sender as ListView; if (view == null) { return; } GridViewSortHandler handler = view.GetValue(GridViewSortHandlerProperty) as GridViewSortHandler; if (handler == null) { return; } //set the parent control of the handler handler.parentControl = view; handler.comparer = view.GetValue(CustomComparerProperty) as GridViewCustomComparer; GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader; // ensure that we clicked on the column header and not the padding that's added to fill the space. if (headerClicked != null && headerClicked.Role != GridViewColumnHeaderRole.Padding) { // attempt to cast to the sortableGridViewColumn object. SortableGridViewColumn sortableGridViewColumn = headerClicked.Column as SortableGridViewColumn; if (sortableGridViewColumn.CanSort) { handler.ApplySort(sortableGridViewColumn); } } }
//registers to the column click of the header to apply the sort private static void RegisterSortHandler(DependencyObject obj, DependencyPropertyChangedEventArgs args) { ListView grid = obj as ListView; if (grid == null) { return; } GridViewSortHandler handler = args.NewValue as GridViewSortHandler; if (handler != null) { //register to click events of column header grid.AddHandler(GridViewColumnHeader.ClickEvent, columnClickhandler); } }
/// <summary> /// Sets the value GridViewSortable on the specified object /// </summary> /// <param name="obj">The object to store the value of the property</param> /// <param name="value">The value to set</param> public static void SetGridViewSortHandler(DependencyObject obj, GridViewSortHandler value) { obj.SetValue(GridViewSortHandlerProperty, value); }