private static void OnSortDirectionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { var grid = sender as ListView; if (grid != null) { SortableGridViewColumn col = GetCurrentSortColumn(grid); if (col != null) { col.SortDirection = (ColumnSortDirection)args.NewValue; } Sort(grid); } }
private static void Sort(ListView grid) { ColumnSortDirection currentDirection = GetSortDirection(grid); SortableGridViewColumn column = GetCurrentSortColumn(grid); if (column != null && GetSortMode(grid) == ListViewSortMode.Automatic && currentDirection != ColumnSortDirection.None) { ICollectionView dataView = CollectionViewSource.GetDefaultView(grid.ItemsSource); string sortBy = column.SortBy; if (sortBy == null) { var binding = column.DisplayMemberBinding as Binding; if (binding != null && binding.Path != null) { sortBy = binding.Path.Path; } } dataView.SortDescriptions.Clear(); if (sortBy != null) { ListSortDirection direction; if (currentDirection == ColumnSortDirection.Descending) { direction = ListSortDirection.Descending; } else { direction = ListSortDirection.Ascending; } dataView.SortDescriptions.Add(new SortDescription(sortBy, direction)); } dataView.Refresh(); } }
public static void SetCurrentSortColumn(ListView listView, SortableGridViewColumn value) { listView.SetValue(CurrentSortColumnProperty, value); }