|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Windows; |
| 4 | +using System.Windows.Controls; |
| 5 | +using System.Windows.Controls.Primitives; |
| 6 | +using System.Windows.Media; |
| 7 | + |
| 8 | +namespace MemPlus.Business.UTILS |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Internal logic for sorting a GridView |
| 12 | + /// </summary> |
| 13 | + internal class GridViewSort |
| 14 | + { |
| 15 | + #region Properties |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Check whether sorting is enabled or not |
| 19 | + /// </summary> |
| 20 | + /// <param name="obj">The DependencyObject that needs to be checked for sorting</param> |
| 21 | + /// <returns>True if sorting is enabled, otherwise false</returns> |
| 22 | + internal static bool GetEnabled(DependencyObject obj) |
| 23 | + { |
| 24 | + return (bool)obj.GetValue(EnabledProperty); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Enable or disable sorting |
| 29 | + /// </summary> |
| 30 | + /// <param name="obj">The DependencyObject that needs to have the Enabled property for sorting changed</param> |
| 31 | + /// <param name="value">True if enabled, otherwise false</param> |
| 32 | + internal static void SetEnabled(DependencyObject obj, bool value) |
| 33 | + { |
| 34 | + obj.SetValue(EnabledProperty, value); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// The EnabledProperty that can be used by DependencyObjects |
| 39 | + /// </summary> |
| 40 | + internal static readonly DependencyProperty EnabledProperty = DependencyProperty.RegisterAttached("Enabled", typeof(bool), typeof(GridViewSort), |
| 41 | + new UIPropertyMetadata( |
| 42 | + false, |
| 43 | + (o, e) => |
| 44 | + { |
| 45 | + // ReSharper disable once UsePatternMatching |
| 46 | + ListView listView = o as ListView; |
| 47 | + if (listView == null) return; |
| 48 | + bool oldValue = (bool)e.OldValue; |
| 49 | + bool newValue = (bool)e.NewValue; |
| 50 | + if (oldValue && !newValue) |
| 51 | + { |
| 52 | + listView.RemoveHandler(ButtonBase.ClickEvent, new RoutedEventHandler(ColumnHeader_Click)); |
| 53 | + } |
| 54 | + if (!oldValue && newValue) |
| 55 | + { |
| 56 | + listView.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(ColumnHeader_Click)); |
| 57 | + } |
| 58 | + } |
| 59 | + ) |
| 60 | + ); |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Get the name of a property |
| 64 | + /// </summary> |
| 65 | + /// <param name="obj">The DependencyObject</param> |
| 66 | + /// <returns>The name of a property</returns> |
| 67 | + internal static string GetPropertyName(DependencyObject obj) |
| 68 | + { |
| 69 | + return (string)obj.GetValue(PropertyNameProperty); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Set the name of a property |
| 74 | + /// </summary> |
| 75 | + /// <param name="obj">The DependencyObject</param> |
| 76 | + /// <param name="value">The name of the property</param> |
| 77 | + internal static void SetPropertyName(DependencyObject obj, string value) |
| 78 | + { |
| 79 | + obj.SetValue(PropertyNameProperty, value); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Using a DependencyProperty as the backing store for PropertyName. This enables animation, styling, binding, etc... |
| 84 | + /// </summary> |
| 85 | + internal static readonly DependencyProperty PropertyNameProperty = |
| 86 | + DependencyProperty.RegisterAttached( |
| 87 | + "PropertyName", |
| 88 | + typeof(string), |
| 89 | + typeof(GridViewSort), |
| 90 | + new UIPropertyMetadata(null) |
| 91 | + ); |
| 92 | + |
| 93 | + #endregion |
| 94 | + |
| 95 | + #region ColumnHeader |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Method that is called when a ColumnHeader object is clicked |
| 99 | + /// </summary> |
| 100 | + /// <param name="sender">The object that called this method</param> |
| 101 | + /// <param name="e">The RoutedEventArgs</param> |
| 102 | + private static void ColumnHeader_Click(object sender, RoutedEventArgs e) |
| 103 | + { |
| 104 | + if (!(e.OriginalSource is GridViewColumnHeader headerClicked)) return; |
| 105 | + string propertyName = GetPropertyName(headerClicked.Column); |
| 106 | + if (string.IsNullOrEmpty(propertyName)) return; |
| 107 | + ListView listView = GetAncestor<ListView>(headerClicked); |
| 108 | + if (listView == null) return; |
| 109 | + if (GetEnabled(listView)) |
| 110 | + { |
| 111 | + ApplySort(listView.Items, propertyName); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + #endregion |
| 116 | + |
| 117 | + #region Helpermethods |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Get the parent object of a DependencyObject |
| 121 | + /// </summary> |
| 122 | + /// <typeparam name="T">DependencyObject</typeparam> |
| 123 | + /// <param name="reference">A DependencyObject</param> |
| 124 | + /// <returns>The parent of a DependencyObject</returns> |
| 125 | + internal static T GetAncestor<T>(DependencyObject reference) where T : DependencyObject |
| 126 | + { |
| 127 | + DependencyObject parent = VisualTreeHelper.GetParent(reference); |
| 128 | + while (!(parent is T)) |
| 129 | + { |
| 130 | + parent = VisualTreeHelper.GetParent(parent ?? throw new InvalidOperationException()); |
| 131 | + } |
| 132 | + return (T)parent; |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Apply the sorting to the content of a ICollectionView |
| 137 | + /// </summary> |
| 138 | + /// <param name="view">The ICollectionView</param> |
| 139 | + /// <param name="propertyName">The name of the property</param> |
| 140 | + internal static void ApplySort(ICollectionView view, string propertyName) |
| 141 | + { |
| 142 | + ListSortDirection direction = ListSortDirection.Ascending; |
| 143 | + if (view.SortDescriptions.Count > 0) |
| 144 | + { |
| 145 | + SortDescription currentSort = view.SortDescriptions[0]; |
| 146 | + if (currentSort.PropertyName == propertyName) |
| 147 | + { |
| 148 | + direction = currentSort.Direction == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending; |
| 149 | + } |
| 150 | + view.SortDescriptions.Clear(); |
| 151 | + } |
| 152 | + if (!string.IsNullOrEmpty(propertyName)) |
| 153 | + { |
| 154 | + view.SortDescriptions.Add(new SortDescription(propertyName, direction)); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + #endregion |
| 159 | + } |
| 160 | +} |
0 commit comments