From 22da0a3151e860d1e04df391adeabb9575c464ef Mon Sep 17 00:00:00 2001 From: Eric Neville Date: Fri, 12 Jun 2026 14:35:49 -0400 Subject: [PATCH] allow sorting based on separate attribute --- Maui.DataGrid/CompatibilitySuppressions.xml | 21 ++++++++++++++++++ Maui.DataGrid/DataGrid.xaml.cs | 8 +++++-- Maui.DataGrid/DataGridColumn.cs | 20 ++++++++++++++++- Maui.DataGrid/packages.lock.json | 24 ++++++++++----------- 4 files changed, 58 insertions(+), 15 deletions(-) diff --git a/Maui.DataGrid/CompatibilitySuppressions.xml b/Maui.DataGrid/CompatibilitySuppressions.xml index eb0d878..e0e9265 100644 --- a/Maui.DataGrid/CompatibilitySuppressions.xml +++ b/Maui.DataGrid/CompatibilitySuppressions.xml @@ -8,6 +8,13 @@ lib/net9.0/Maui.DataGrid.dll true + + CP0002 + F:Maui.DataGrid.DataGridColumn.SortPropertyNameProperty + lib/net9.0/Maui.DataGrid.dll + lib/net9.0/Maui.DataGrid.dll + true + CP0002 M:Maui.DataGrid.DataGridColumn.get_Padding @@ -15,6 +22,13 @@ lib/net9.0/Maui.DataGrid.dll true + + CP0002 + M:Maui.DataGrid.DataGridColumn.get_SortPropertyName + lib/net9.0/Maui.DataGrid.dll + lib/net9.0/Maui.DataGrid.dll + true + CP0002 M:Maui.DataGrid.DataGridColumn.set_Padding(Microsoft.Maui.Thickness) @@ -22,6 +36,13 @@ lib/net9.0/Maui.DataGrid.dll true + + CP0002 + M:Maui.DataGrid.DataGridColumn.set_SortPropertyName(System.String) + lib/net9.0/Maui.DataGrid.dll + lib/net9.0/Maui.DataGrid.dll + true + PKV006 net8.0 diff --git a/Maui.DataGrid/DataGrid.xaml.cs b/Maui.DataGrid/DataGrid.xaml.cs index 81405b3..eb1b109 100644 --- a/Maui.DataGrid/DataGrid.xaml.cs +++ b/Maui.DataGrid/DataGrid.xaml.cs @@ -1471,6 +1471,10 @@ private IEnumerable GetSortedItems(IList unsortedItems, SortData IEnumerable items; + string sortPropertyName = string.IsNullOrEmpty(_sortedColumn.SortPropertyName) + ? _sortedColumn.PropertyName + : _sortedColumn.SortPropertyName; + switch (sortData.Order) { case SortingOrder.Ascendant: @@ -1479,7 +1483,7 @@ private IEnumerable GetSortedItems(IList unsortedItems, SortData #else _ = _sortedColumn.SortingIcon.RotateTo(0); #endif - items = unsortedItems.OrderBy(x => x.GetValueByPath(_sortedColumn.PropertyName)); + items = unsortedItems.OrderBy(x => x.GetValueByPath(sortPropertyName)); break; case SortingOrder.Descendant: #if NET10_0_OR_GREATER @@ -1487,7 +1491,7 @@ private IEnumerable GetSortedItems(IList unsortedItems, SortData #else _ = _sortedColumn.SortingIcon.RotateTo(180); #endif - items = unsortedItems.OrderByDescending(x => x.GetValueByPath(_sortedColumn.PropertyName)); + items = unsortedItems.OrderByDescending(x => x.GetValueByPath(sortPropertyName)); break; case SortingOrder.None: return unsortedItems; diff --git a/Maui.DataGrid/DataGridColumn.cs b/Maui.DataGrid/DataGridColumn.cs index 4a02fa5..a6e96c9 100644 --- a/Maui.DataGrid/DataGridColumn.cs +++ b/Maui.DataGrid/DataGridColumn.cs @@ -210,6 +210,12 @@ public sealed class DataGridColumn : BindableObject, IDefinition } }); + /// + /// Gets or sets the name of the sort property associated with the column. + /// + public static readonly BindableProperty SortPropertyNameProperty = + BindablePropertyExtensions.Create(); + #endregion Bindable Properties #region Fields @@ -443,6 +449,15 @@ public Style HeaderFilterStyle set => SetValue(HeaderFilterStyleProperty, value); } + /// + /// Gets or sets sort property name to bind in the object. + /// + public string SortPropertyName + { + get => (string)GetValue(SortPropertyNameProperty); + set => SetValue(SortPropertyNameProperty, value); + } + internal Polygon SortingIcon { get; } = new(); internal Entry FilterTextbox { get; } = new() { Placeholder = "Filter" }; @@ -466,6 +481,8 @@ public Style HeaderFilterStyle internal Type? DataType { get; private set; } + internal Type? SortDataType { get; private set; } + internal DataGrid? DataGrid { get; set; } internal ColumnDefinition? ColumnDefinition @@ -502,7 +519,7 @@ public bool IsSortable() } else if (DataType is not null) { - _isSortable = typeof(IComparable).IsAssignableFrom(DataType); + _isSortable = typeof(IComparable).IsAssignableFrom(SortDataType); } return _isSortable ??= false; @@ -544,6 +561,7 @@ internal void InitializeDataType() } DataType = rowDataType?.GetPropertyTypeByPath(PropertyName); + SortDataType = string.IsNullOrEmpty(SortPropertyName) ? DataType : rowDataType?.GetPropertyTypeByPath(SortPropertyName); } catch (Exception ex) when (ex is NotSupportedException or ArgumentNullException or InvalidCastException) diff --git a/Maui.DataGrid/packages.lock.json b/Maui.DataGrid/packages.lock.json index 2207cb3..9b92846 100644 --- a/Maui.DataGrid/packages.lock.json +++ b/Maui.DataGrid/packages.lock.json @@ -10,9 +10,9 @@ }, "Microsoft.DotNet.ILCompiler": { "type": "Direct", - "requested": "[10.0.2, )", - "resolved": "10.0.2", - "contentHash": "0g8tazNW4q0anSYNl1hnaHGc7+BQXwpNBMx3d5T3JboOVzz4OXZgqbuoBYfn60VtDFtBMNVS2Kfopelpo2pLZA==" + "requested": "[10.0.3, )", + "resolved": "10.0.3", + "contentHash": "H9kuN7egYzW5JAn5CMK8uJdJDVnPEDpZHzjOXyz1iAgvxNgVSoAiTNkCBeY2jaXcHlruF5fRUrZoUHUHXftDDA==" }, "Microsoft.Maui.Controls": { "type": "Direct", @@ -28,9 +28,9 @@ }, "Microsoft.NET.ILLink.Tasks": { "type": "Direct", - "requested": "[10.0.2, )", - "resolved": "10.0.2", - "contentHash": "sXdDtMf2qcnbygw9OdE535c2lxSxrZP8gO4UhDJ0xiJbl1wIqXS1OTcTDFTIJPOFd6Mhcm8gPEthqWGUxBsTqw==" + "requested": "[10.0.8, )", + "resolved": "10.0.8", + "contentHash": "dVbSXGIFNR5nZcv2tOLoWI+a9T4jtFd77IYjuND+QVe360qWgAF7H0WtoopYhRw/+SgpGUTyrkrh+65+ClNnfw==" }, "StyleCop.Analyzers": { "type": "Direct", @@ -210,9 +210,9 @@ }, "Microsoft.DotNet.ILCompiler": { "type": "Direct", - "requested": "[9.0.12, )", - "resolved": "9.0.12", - "contentHash": "6RjjgSgfaKgnhNepPhfKQFB/g2TzbqmuOR29jWuOfeM5HMIyELd2T/OQC6P7gKEaOMYp3iYwt21dyYgWVpqnVQ==" + "requested": "[9.0.13, )", + "resolved": "9.0.13", + "contentHash": "HjOJCDy5GcOzkOeQuBLgbu8nUBvoZIZ+yA8SkDbb00PLAtYlRyZyfjjyNuhj3C0k+aliAV1oUCzVXqRQFxeI8g==" }, "Microsoft.Maui.Controls": { "type": "Direct", @@ -228,9 +228,9 @@ }, "Microsoft.NET.ILLink.Tasks": { "type": "Direct", - "requested": "[9.0.12, )", - "resolved": "9.0.12", - "contentHash": "StA3kyImQHqDo8A8ZHaSxgASbEuT5UIqgeCvK5SzUPj//xE1QSys421J9pEs4cYuIVwq7CJvWSKxtyH7aPr1LA==" + "requested": "[9.0.16, )", + "resolved": "9.0.16", + "contentHash": "ccPBYGLPJt8DeJTUzQ0JzOh/iuUAgnjayU63PokVywAhUOx+dzDKSPTL7AG94U/VpvNXflTT2AjsFAIF1+bXBw==" }, "StyleCop.Analyzers": { "type": "Direct",