Skip to content

Commit

Permalink
re-use sort command
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Mar 28, 2024
1 parent 65d84a4 commit 64ae536
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions Maui.DataGrid/DataGridHeaderRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ internal sealed class DataGridHeaderRow : Grid

private readonly Thickness _headerCellPadding = new(0, 0, 4, 0);

private readonly Command<DataGridColumn> _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
Expand Down Expand Up @@ -170,21 +191,7 @@ private DataGridCell CreateHeaderCell(DataGridColumn column)
{
new TapGestureRecognizer
{
Command = new Command<DataGridColumn>(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
}
}
Expand Down

0 comments on commit 64ae536

Please sign in to comment.