|
| 1 | +namespace Gu.Wpf.DataGrid2D |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.ComponentModel; |
| 5 | + using System.Windows; |
| 6 | + using System.Windows.Controls; |
| 7 | + using System.Windows.Input; |
| 8 | + |
| 9 | + public class Sort |
| 10 | + { |
| 11 | + public static readonly DependencyProperty ThreeStateProperty = DependencyProperty.RegisterAttached( |
| 12 | + "ThreeState", |
| 13 | + typeof(bool), |
| 14 | + typeof(Sort), |
| 15 | + new PropertyMetadata(default(bool), OnThreeStateChanged)); |
| 16 | + |
| 17 | + private static readonly DependencyProperty SubscriptionProperty = DependencyProperty.RegisterAttached( |
| 18 | + "Subscription", |
| 19 | + typeof(SortingSubscription), |
| 20 | + typeof(Sort), |
| 21 | + new PropertyMetadata(default(SortingSubscription))); |
| 22 | + |
| 23 | + public static void SetThreeState(DependencyObject element, bool value) |
| 24 | + { |
| 25 | + element.SetValue(ThreeStateProperty, value); |
| 26 | + } |
| 27 | + |
| 28 | + public static bool GetThreeState(DependencyObject element) |
| 29 | + { |
| 30 | + return (bool)element.GetValue(ThreeStateProperty); |
| 31 | + } |
| 32 | + |
| 33 | + private static void OnThreeStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 34 | + { |
| 35 | + if ((bool)e.NewValue) |
| 36 | + { |
| 37 | + d.SetCurrentValue(SubscriptionProperty, new SortingSubscription((DataGrid)d)); |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + (d.GetValue(SubscriptionProperty) as IDisposable)?.Dispose(); |
| 42 | + d.ClearValue(SubscriptionProperty); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static void OnDataGridSorting(object sender, DataGridSortingEventArgs e) |
| 47 | + { |
| 48 | + var dataGrid = (DataGrid)sender; |
| 49 | + var sortPropertyName = e.Column.SortMemberPath; |
| 50 | + if (!string.IsNullOrEmpty(sortPropertyName)) |
| 51 | + { |
| 52 | + // sorting is cleared when the previous state is Descending |
| 53 | + if (e.Column.SortDirection.HasValue && e.Column.SortDirection.Value == ListSortDirection.Descending) |
| 54 | + { |
| 55 | + e.Column.SetCurrentValue(DataGridColumn.SortDirectionProperty, null); |
| 56 | + if ((Keyboard.Modifiers & ModifierKeys.Shift) != ModifierKeys.Shift) |
| 57 | + { |
| 58 | + var sortDescriptions = dataGrid.Items.SortDescriptions; |
| 59 | + for (var i = sortDescriptions.Count - 1; i >= 0; i--) |
| 60 | + { |
| 61 | + if (sortDescriptions[i].PropertyName == sortPropertyName) |
| 62 | + { |
| 63 | + sortDescriptions.RemoveAt(i); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + dataGrid.Items.SortDescriptions.Clear(); |
| 70 | + } |
| 71 | + |
| 72 | + dataGrid.Items.Refresh(); |
| 73 | + e.Handled = true; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private sealed class SortingSubscription : IDisposable |
| 79 | + { |
| 80 | + private readonly DataGrid dataGrid; |
| 81 | + |
| 82 | + public SortingSubscription(DataGrid dataGrid) |
| 83 | + { |
| 84 | + this.dataGrid = dataGrid; |
| 85 | + this.dataGrid.Sorting += OnDataGridSorting; |
| 86 | + } |
| 87 | + |
| 88 | + public void Dispose() |
| 89 | + { |
| 90 | + this.dataGrid.Sorting -= OnDataGridSorting; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments