diff --git a/src/Avalonia.Controls.DataGrid/DataGridColumn.cs b/src/Avalonia.Controls.DataGrid/DataGridColumn.cs
index 4ab2869138..5499257171 100644
--- a/src/Avalonia.Controls.DataGrid/DataGridColumn.cs
+++ b/src/Avalonia.Controls.DataGrid/DataGridColumn.cs
@@ -9,6 +9,7 @@ using Avalonia.VisualTree;
using Avalonia.Collections;
using Avalonia.Utilities;
using System;
+using System.ComponentModel;
using System.Linq;
using System.Diagnostics;
using Avalonia.Controls.Utils;
@@ -653,6 +654,34 @@ namespace Avalonia.Controls
return null;
}
+ ///
+ /// Clears the current sort direction
+ ///
+ public void ClearSort()
+ {
+ //InvokeProcessSort is already validating if sorting is possible
+ _headerCell?.InvokeProcessSort(Input.KeyModifiers.Control);
+ }
+
+ ///
+ /// Switches the current state of sort direction
+ ///
+ public void Sort()
+ {
+ //InvokeProcessSort is already validating if sorting is possible
+ _headerCell?.InvokeProcessSort(Input.KeyModifiers.None);
+ }
+
+ ///
+ /// Changes the sort direction of this column
+ ///
+ /// New sort direction
+ public void Sort(ListSortDirection direction)
+ {
+ //InvokeProcessSort is already validating if sorting is possible
+ _headerCell?.InvokeProcessSort(Input.KeyModifiers.None, direction);
+ }
+
///
/// When overridden in a derived class, causes the column cell being edited to revert to the unedited value.
///
diff --git a/src/Avalonia.Controls.DataGrid/DataGridColumnHeader.cs b/src/Avalonia.Controls.DataGrid/DataGridColumnHeader.cs
index 6f957497cb..85fd55800a 100644
--- a/src/Avalonia.Controls.DataGrid/DataGridColumnHeader.cs
+++ b/src/Avalonia.Controls.DataGrid/DataGridColumnHeader.cs
@@ -201,21 +201,21 @@ namespace Avalonia.Controls
handled = true;
}
- internal void InvokeProcessSort(KeyModifiers keyModifiers)
+ internal void InvokeProcessSort(KeyModifiers keyModifiers, ListSortDirection? forcedDirection = null)
{
Debug.Assert(OwningGrid != null);
- if (OwningGrid.WaitForLostFocus(() => InvokeProcessSort(keyModifiers)))
+ if (OwningGrid.WaitForLostFocus(() => InvokeProcessSort(keyModifiers, forcedDirection)))
{
return;
}
if (OwningGrid.CommitEdit(DataGridEditingUnit.Row, exitEditingMode: true))
{
- Avalonia.Threading.Dispatcher.UIThread.Post(() => ProcessSort(keyModifiers));
+ Avalonia.Threading.Dispatcher.UIThread.Post(() => ProcessSort(keyModifiers, forcedDirection));
}
}
//TODO GroupSorting
- internal void ProcessSort(KeyModifiers keyModifiers)
+ internal void ProcessSort(KeyModifiers keyModifiers, ListSortDirection? forcedDirection = null)
{
// if we can sort:
// - AllowUserToSortColumns and CanSort are true, and
@@ -259,7 +259,14 @@ namespace Avalonia.Controls
{
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
// 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)
{
- newSort = DataGridSortDescription.FromComparer(OwningColumn.CustomSortComparer);
+ newSort = forcedDirection != null ?
+ DataGridSortDescription.FromComparer(OwningColumn.CustomSortComparer, forcedDirection.Value) :
+ DataGridSortDescription.FromComparer(OwningColumn.CustomSortComparer);
+
owningGrid.DataConnection.SortDescriptions.Add(newSort);
}
@@ -290,6 +300,10 @@ namespace Avalonia.Controls
}
newSort = DataGridSortDescription.FromPath(propertyName, culture: collectionView.Culture);
+ if (forcedDirection != null && newSort.Direction != forcedDirection)
+ {
+ newSort = newSort.SwitchSortDirection();
+ }
owningGrid.DataConnection.SortDescriptions.Add(newSort);
}