From 64ae536578dc8a6e7b8d3991ab2bacbca346ad58 Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Thu, 28 Mar 2024 14:48:23 -0500 Subject: [PATCH] re-use sort command --- Maui.DataGrid/DataGridHeaderRow.cs | 37 ++++++++++++++++++------------ 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/Maui.DataGrid/DataGridHeaderRow.cs b/Maui.DataGrid/DataGridHeaderRow.cs index f820fda..3cfe878 100644 --- a/Maui.DataGrid/DataGridHeaderRow.cs +++ b/Maui.DataGrid/DataGridHeaderRow.cs @@ -15,6 +15,27 @@ internal sealed class DataGridHeaderRow : Grid private readonly Thickness _headerCellPadding = new(0, 0, 4, 0); + private readonly Command _sortCommand = new(c => + { + ArgumentNullException.ThrowIfNull(c.DataGrid); + + // This is to invert SortOrder when the user taps on a column. + var order = c.SortingOrder == SortingOrder.Ascendant + ? SortingOrder.Descendant + : SortingOrder.Ascendant; + + var index = c.DataGrid.Columns.IndexOf(c); + + c.DataGrid.SortedColumnIndex = new(index, order); + + c.SortingOrder = order; + }, c => + { + ArgumentNullException.ThrowIfNull(c.DataGrid); + + return c.SortingEnabled && c.DataGrid.Columns.Contains(c); + }); + #endregion Fields #region Properties @@ -170,21 +191,7 @@ private DataGridCell CreateHeaderCell(DataGridColumn column) { new TapGestureRecognizer { - Command = new Command(c => - { - ArgumentNullException.ThrowIfNull(c.DataGrid); - - // This is to invert SortOrder when the user taps on a column. - var order = c.SortingOrder == SortingOrder.Ascendant - ? SortingOrder.Descendant - : SortingOrder.Ascendant; - - var index = c.DataGrid.Columns.IndexOf(c); - - c.DataGrid.SortedColumnIndex = new(index, order); - - c.SortingOrder = order; - }, c => c.SortingEnabled && DataGrid.Columns.Contains(c)), + Command = _sortCommand, CommandParameter = column } }