Browse Source

Update DataGridColumnHeader.cs

pull/6730/head
0x90d 5 years ago
committed by GitHub
parent
commit
68a3f7fa97
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      src/Avalonia.Controls.DataGrid/DataGridColumnHeader.cs

26
src/Avalonia.Controls.DataGrid/DataGridColumnHeader.cs

@ -201,21 +201,21 @@ namespace Avalonia.Controls
handled = true; handled = true;
} }
internal void InvokeProcessSort(KeyModifiers keyModifiers) internal void InvokeProcessSort(KeyModifiers keyModifiers, ListSortDirection? forcedDirection = null)
{ {
Debug.Assert(OwningGrid != null); Debug.Assert(OwningGrid != null);
if (OwningGrid.WaitForLostFocus(() => InvokeProcessSort(keyModifiers))) if (OwningGrid.WaitForLostFocus(() => InvokeProcessSort(keyModifiers, forcedDirection)))
{ {
return; return;
} }
if (OwningGrid.CommitEdit(DataGridEditingUnit.Row, exitEditingMode: true)) if (OwningGrid.CommitEdit(DataGridEditingUnit.Row, exitEditingMode: true))
{ {
Avalonia.Threading.Dispatcher.UIThread.Post(() => ProcessSort(keyModifiers)); Avalonia.Threading.Dispatcher.UIThread.Post(() => ProcessSort(keyModifiers, forcedDirection));
} }
} }
//TODO GroupSorting //TODO GroupSorting
internal void ProcessSort(KeyModifiers keyModifiers) internal void ProcessSort(KeyModifiers keyModifiers, ListSortDirection? forcedDirection = null)
{ {
// if we can sort: // if we can sort:
// - AllowUserToSortColumns and CanSort are true, and // - AllowUserToSortColumns and CanSort are true, and
@ -259,7 +259,14 @@ namespace Avalonia.Controls
{ {
if (sort != null) if (sort != null)
{ {
newSort = sort.SwitchSortDirection(); if (forcedDirection == null || sort.Direction != forcedDirection)
{
newSort = sort.SwitchSortDirection();
}
else
{
newSort = sort;
}
// changing direction should not affect sort order, so we replace this column's // changing direction should not affect sort order, so we replace this column's
// sort description instead of just adding it to the end of the collection // sort description instead of just adding it to the end of the collection
@ -276,7 +283,10 @@ namespace Avalonia.Controls
} }
else if (OwningColumn.CustomSortComparer != null) else if (OwningColumn.CustomSortComparer != null)
{ {
newSort = DataGridSortDescription.FromComparer(OwningColumn.CustomSortComparer); newSort = forcedDirection != null ?
DataGridSortDescription.FromComparer(OwningColumn.CustomSortComparer, forcedDirection.Value) :
DataGridSortDescription.FromComparer(OwningColumn.CustomSortComparer);
owningGrid.DataConnection.SortDescriptions.Add(newSort); owningGrid.DataConnection.SortDescriptions.Add(newSort);
} }
@ -290,6 +300,10 @@ namespace Avalonia.Controls
} }
newSort = DataGridSortDescription.FromPath(propertyName, culture: collectionView.Culture); newSort = DataGridSortDescription.FromPath(propertyName, culture: collectionView.Culture);
if (forcedDirection != null && newSort.Direction != forcedDirection)
{
newSort = newSort.SwitchSortDirection();
}
owningGrid.DataConnection.SortDescriptions.Add(newSort); owningGrid.DataConnection.SortDescriptions.Add(newSort);
} }

Loading…
Cancel
Save