From f6c7d7a72ba2cdd2a35704a06221eccb6d997cb7 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Sun, 17 Mar 2024 22:14:49 +0800 Subject: [PATCH] enhance: add IsTristateSortingEnabled for DataGridAttach. --- .../Controls/Attach/DataGridAttach.cs | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs index eee36941..922f4449 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs @@ -1,4 +1,5 @@ using System.Collections.Specialized; +using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Controls; @@ -342,4 +343,62 @@ public class DataGridAttach public static bool GetShowSelectAllButton(DependencyObject element) => (bool) element.GetValue(ShowSelectAllButtonProperty); + + public static readonly DependencyProperty IsTristateSortingEnabledProperty = DependencyProperty.RegisterAttached( + "IsTristateSortingEnabled", typeof(bool), typeof(DataGridAttach), new PropertyMetadata( + ValueBoxes.FalseBox, OnIsTristateSortingEnabledChanged)); + + private static void OnIsTristateSortingEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is not DataGrid dataGrid) + { + return; + } + + if ((bool) e.NewValue) + { + dataGrid.Sorting += DataGridOnSorting; + } + else + { + dataGrid.Sorting -= DataGridOnSorting; + } + } + + private static void DataGridOnSorting(object sender, DataGridSortingEventArgs e) + { + if (sender is not DataGrid dataGrid || + string.IsNullOrEmpty(e.Column.SortMemberPath) || + e.Column.SortDirection is not ListSortDirection.Descending) + { + return; + } + + var description = dataGrid.Items + .SortDescriptions + .FirstOrDefault(item => string.Equals(item.PropertyName, e.Column.SortMemberPath)); + var index = dataGrid.Items.SortDescriptions.IndexOf(description); + if (index == -1) + { + return; + } + + e.Column.SortDirection = null; + dataGrid.Items.SortDescriptions.RemoveAt(index); + dataGrid.Items.Refresh(); + + if ((Keyboard.Modifiers & ModifierKeys.Shift) != ModifierKeys.Shift) + { + dataGrid.Items.SortDescriptions.Clear(); + dataGrid.Items.Refresh(); + } + + e.Handled = true; + } + + public static void SetIsTristateSortingEnabled(DependencyObject element, bool value) + => element.SetValue(IsTristateSortingEnabledProperty, ValueBoxes.BooleanBox(value)); + + public static bool GetIsTristateSortingEnabled(DependencyObject element) + => (bool) element.GetValue(IsTristateSortingEnabledProperty); }