Skip to content

Commit

Permalink
enhance: add IsTristateSortingEnabled for DataGridAttach.
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Mar 17, 2024
1 parent 454434e commit f6c7d7a
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -342,4 +343,62 @@ public static void SetShowSelectAllButton(DependencyObject element, bool value)

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);
}

0 comments on commit f6c7d7a

Please sign in to comment.