allow sorting based on separate attribute - #239
Conversation
|
Hi, sorry for the lack of response. Could you explain more about what the purpose of this is? Maybe provide an example of how and why one would want this? Seems like the kind of thing that you could work around using logic in your ViewModel, and may not be worth expanding the API of the datagrid for. |
|
For instance, if you have a complex object in the column, and you want to sort on that complex object, then inheriting that object from No need to modify the ViewModel, even. |
That might very well be. My issue was that I needed the whole object to present my content template appropriately (bold vs not bold based on read status), so I bind to ".", but I still want to make sure the column itself sorts by the appropriate property in the underlying object. If there's a more elegant way of doing something like that, I'm certainly open to it. My applicable view code is as follows, if you're curious.
|
|
You would just leave off the SortPropertyName and make BroadcastMessageEntry inherit from IComparable public class BroadcastMessageEntry : IComparable<BroadcastMessageEntry>, IComparable
{
public DateTime SentDateTimeClean { get; set; }
public int CompareTo(BroadcastMessageEntry? other)
{
if (other is null)
{
return 1;
}
return SentDateTimeClean.CompareTo(other.SentDateTimeClean);
}
public int CompareTo(object? obj)
{
if (obj is null)
{
return 1;
}
if (obj is not BroadcastMessageEntry other)
{
throw new ArgumentException($"Object must be of type {nameof(BroadcastMessageEntry)}.", nameof(obj));
}
return CompareTo(other);
}
} |
|
How does that help sort columns by properties of that object?
…________________________________
From: Edward Miller ***@***.***>
Sent: Sunday, 12 July 2026 12:58:58
To: akgulebubekir/Maui.DataGrid ***@***.***>
Cc: Eric Neville ***@***.***>; Author ***@***.***>
Subject: Re: [akgulebubekir/Maui.DataGrid] allow sorting based on separate attribute (PR #239)
EXTERNAL EMAIL - Use Caution
Closed #239<#239>.
—
Reply to this email directly, view it on GitHub<#239?email_source=notifications&email_token=BWQH2UXNBLYA2ESUNYCCSVT5EO7VFA5CNFSNUABQM5UWIORPF5TWS5BNNB2WEL2JONZXKZKFOZSW45CON52GSZTJMNQXI2LPNYXTENZYG43TIMRQHA4TRJTSMVQXG33OUZQXK5DIN5ZKKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#event-27877420898>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BWQH2UVWBPRBWROTJAA2OHL5EO7VFAVCNFSNUABFKJSXA33TNF2G64TZHM2TENRVGQZDGNZWHNEXG43VMU5TINRVGE4DSOBRGIY2C5QC>.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS<https://github.com/notifications/mobile/ios/BWQH2URCMT3Y3WHQRCLC7VT5EO7VFA5CNFSNUABQM5UWIORPF5TWS5BNNB2WEL2JONZXKZKFOZSW45CON52GSZTJMNQXI2LPNYXTENZYG43TIMRQHA4TRJTSMVQXG33OUZQXK5DIN5ZKKZLWMVXHJKTGN5XXIZLSL5UW64Y> and Android<https://github.com/notifications/mobile/android/BWQH2UU7BC5S6NG2DI6BADT5EO7VFA5CNFSNUABQM5UWIORPF5TWS5BNNB2WEL2JONZXKZKFOZSW45CON52GSZTJMNQXI2LPNYXTENZYG43TIMRQHA4TRJTSMVQXG33OUZQXK5DIN5ZKKZLWMVXHJLTGN5XXIZLSL5QW4ZDSN5UWI>. Download it today!
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
|
IComparable allows you to treat a C# class as if it were sortable like a number. LINQ methods like OrderBy operate on IComparable which is implemented by number types and can also be implemented by classes. My code above provides the ability to sort on the nested DateTime property that you showed in your snippet. |
|
Understood, and thank you for your response.
ItemsSource is a List of BroadcastMessages, and the sorts are properties on that object. In that sense, DataGrid works great. But to use triggers for styling etc, I need to bind to the "." and use properties in the triggers as you saw,
The only example I saw for dot-binding with the DataGrid disabled sorting for that column. I'm unclear how I'd use IComparable to sort dynamically based on which column header was clicked.
…________________________________
From: Edward Miller ***@***.***>
Sent: Monday, 13 July 2026 00:29:55
To: akgulebubekir/Maui.DataGrid ***@***.***>
Cc: Eric Neville ***@***.***>; Author ***@***.***>
Subject: Re: [akgulebubekir/Maui.DataGrid] allow sorting based on separate attribute (PR #239)
EXTERNAL EMAIL - Use Caution
[https://avatars.githubusercontent.com/u/1724472?s=20&v=4]symbiogenesis left a comment (akgulebubekir/Maui.DataGrid#239)<#239 (comment)>
IComparable allows you to treat a C# class as if it were sortable like a number. LINQ methods like OrderBy operate on IComparable which is implemented by number types and can also be implemented by classes. My code above provides the ability to sort on the nested DateTime property that you showed in your snippet.
—
Reply to this email directly, view it on GitHub<#239?email_source=notifications&email_token=BWQH2USSDC6G5OJV3YIR6635ERQUHA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIOJVGQ2DINBSGUZ2M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-4954444253>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BWQH2UUYST2Y4P4CLLSS2C35ERQUHAVCNFSNUABFKJSXA33TNF2G64TZHM2TENRVGQZDGNZWHNEXG43VMU5TINRVGE4DSOBRGIY2C5QC>.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS<https://github.com/notifications/mobile/ios/BWQH2UUD2DV77YIXASIJM3T5ERQUHA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIOJVGQ2DINBSGUZ2M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KUZTPN52GK4S7NFXXG> and Android<https://github.com/notifications/mobile/android/BWQH2UQELNJ635YZUYZB4TT5ERQUHA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIOJVGQ2DINBSGUZ2M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2K4ZTPN52GK4S7MFXGI4TPNFSA>. Download it today!
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
|
Just inherit the BroadcastMessageEntry class from IComparable, as I showed. Then, the DataGrid will magically sort on the same property you were targeting, which is SentDateTimeClean. The logic in the method overrides can be modified to change how sorting works. IComparable is infinitely flexible because the sorting can be any algorithm that you think of, but in this case it is just sorting on a DateTime in a normal way. |
|
I'm feeling super dense here, but that column in my example was only one of 3 in my grid, so I'm missing how to use IComparable to handle the sort based on which column in the grid is clicked. I simplified the template below, but this is the gist.
I'll brush up on IComparable, I just didn't see how it would translate to dynamically comparing a different property in the row-bound BroadcastMessage based on which column header happened to be clicked.
<dataGrid:DataGridColumn PropertyName=".">
<dataGrid:DataGridColumn.CellTemplate>
<DataTemplate x:DataType="models:BroadcastMessageEntry">
<Label Text="{Binding SentDateTimeClean}" />
...
</DataTemplate>
</dataGrid:DataGridColumn.CellTemplate>
</dataGrid:DataGridColumn>
<dataGrid:DataGridColumn PropertyName=".">
<dataGrid:DataGridColumn.CellTemplate>
<DataTemplate x:DataType="models:BroadcastMessageEntry">
<Label Text="{Binding ShortSubject}" />
...
</DataTemplate>
</dataGrid:DataGridColumn.CellTemplate>
</dataGrid:DataGridColumn>
<dataGrid:DataGridColumn PropertyName=".">
<dataGrid:DataGridColumn.CellTemplate>
<DataTemplate x:DataType="models:BroadcastMessageEntry">
<Label Text="{Binding ShortMessage}" />
...
</DataTemplate>
</dataGrid:DataGridColumn.CellTemplate>
</dataGrid:DataGridColumn>
…________________________________
From: Edward Miller ***@***.***>
Sent: Tuesday, July 14, 2026 9:35 AM
To: akgulebubekir/Maui.DataGrid ***@***.***>
Cc: Eric Neville ***@***.***>; Author ***@***.***>
Subject: Re: [akgulebubekir/Maui.DataGrid] allow sorting based on separate attribute (PR #239)
EXTERNAL EMAIL - Use Caution
[https://outlook.office.com/mail/inbox/id/AAkALgAAAAAAHYQDEapmEc2byACqAC%2FEWg0AMlDn4T45HU2r0iWBDFGVaQAHyLwBowAA?nativeVersion=1.2026.630.300]symbiogenesis left a comment (akgulebubekir/Maui.DataGrid#239)<#239 (comment)>
Just inherit the BroadcastMessageEntry class from IComparable, as I showed.
Then, the DataGrid will magically sort on the same property you were targeting, which is SentDateTimeClean.
The logic in the method overrides can be modified to change how sorting works.
IComparable is infinitely flexible because the sorting can be any algorithm that you think of, but in this case it is just sorting on a DateTime in a normal way.
—
Reply to this email directly, view it on GitHub<#239?email_source=notifications&email_token=BWQH2UWO62KA5DLNMG5D4ZL5EYZKNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIOJWHE3TKOBSHAYKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-4969758280>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BWQH2UXGWNSKRUCKWT3IHO35EYZKNAVCNFSNUABFKJSXA33TNF2G64TZHM2TENRVGQZDGNZWHNEXG43VMU5TINRVGE4DSOBRGIY2C5QC>.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS<https://github.com/notifications/mobile/ios/BWQH2UQ4QVEJU5RRJFNC4635EYZKNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIOJWHE3TKOBSHAYKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KUZTPN52GK4S7NFXXG> and Android<https://github.com/notifications/mobile/android/BWQH2UX3WCOXHCYD763XE2T5EYZKNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIOJWHE3TKOBSHAYKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2K4ZTPN52GK4S7MFXGI4TPNFSA>. Download it today!
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
|
Ok, I see now that you want multiple columns based on one nested object. In that case I would recommend assigning the ItemsSource to a custom DTO (or custom nested ViewModel) Projecting to a new class is correct in this case and provides better encapsulation and potentially better efficiency. IComparable is more like if you wanted a complex object to be sortable based on custom sorting logic. Sorting based on a name property or date property, for example. But to project multiple nested properties up a level to work as columns, you should use a DTO (or ViewModel) Just define a class with all the columns you are looking for, and make sure that class is filled with the appropriate data. |
|
The "multiple columns based on one [nested] object" is precisely the use case for most if not all DataGrids. One has a collection of items with properties, and we bind to those properties. Your DataGrid already does that fantastically, and I'm using it as such in lots of places.
Where it doesn't quite fit was when I needed to specifically style items in my cell template based on another property in the ItemsSource, even though that property was also available on that row. In that case, I had no choice but to bind to "." and use a CellTemplate and DataTriggers to style the items.
The issue isn't the class; in fact, I'm already projecting to a new class to build this grid in the first place. It's that I can't access properties on said class to style the items in the grid, unless I dot-bind, unless I'm missing something. And if I dot-bind, I can't sort as noted.
My cell template, as noted is as follows:
<dataGrid:DataGridColumn.CellTemplate>
<DataTemplate x:DataType="models:BroadcastMessageEntry">
<ContentView>
<Label Text="{Binding SentDateTimeClean}" FontSize="{Binding FontSize, FallbackValue='14'}" Margin="2">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding IsRead}" Value="False">
<Setter Property="FontAttributes" Value="Bold"/>
</DataTrigger>
</Label.Triggers>
</Label>
</ContentView>
</DataTemplate>
</dataGrid:DataGridColumn.CellTemplate>
…________________________________
From: Edward Miller ***@***.***>
Sent: Tuesday, July 14, 2026 1:56 PM
To: akgulebubekir/Maui.DataGrid ***@***.***>
Cc: Eric Neville ***@***.***>; Author ***@***.***>
Subject: Re: [akgulebubekir/Maui.DataGrid] allow sorting based on separate attribute (PR #239)
EXTERNAL EMAIL - Use Caution
[https://outlook.office.com/mail/inbox/id/AAkALgAAAAAAHYQDEapmEc2byACqAC%2FEWg0AMlDn4T45HU2r0iWBDFGVaQAHyLwbUwAA?nativeVersion=1.2026.630.300]symbiogenesis left a comment (akgulebubekir/Maui.DataGrid#239)<#239 (comment)>
Ok, I see now that you want multiple columns based on one nested object.
In that case I would recommend assigning the ItemsSource to a custom DTO (or custom nested ViewModel)
Projecting to a new class is correct in this case and provides better encapsulation and potentially better efficiency.
IComparable is more like if you wanted a complex object to be sortable based on custom sorting logic. Sorting based on a name property or date property, for example.
But to project multiple nested properties up a level to work as columns, you should use a DTO (or ViewModel)
Just define a class with all the columns you are looking for, and make sure that class is filled with the appropriate data.
—
Reply to this email directly, view it on GitHub<#239?email_source=notifications&email_token=BWQH2UX3ZCRX72YEIHIGSO35EZX4TA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIOJXGIZTGNJSGQYKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-4972335240>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BWQH2UUPGEGMDCJIEANZH5L5EZX4TAVCNFSNUABFKJSXA33TNF2G64TZHM2TENRVGQZDGNZWHNEXG43VMU5TINRVGE4DSOBRGIY2C5QC>.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS<https://github.com/notifications/mobile/ios/BWQH2UT6XNUL2MBSFRND2CL5EZX4TA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIOJXGIZTGNJSGQYKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KUZTPN52GK4S7NFXXG> and Android<https://github.com/notifications/mobile/android/BWQH2UTL6JR2OLRJHP2EOC35EZX4TA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIOJXGIZTGNJSGQYKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2K4ZTPN52GK4S7MFXGI4TPNFSA>. Download it today!
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
|
Ok, I think I have a handle on this, now. Sorting on multiple non-nested properties: works Sorting on multiple nested properties via the dot syntax: works Sorting on a single complex custom column type without the dot syntax: works via IComparable Sorting on multiple complex custom columns of the same type without the dot syntax: not possible in this library My reflexive opposition was based on the idea that if you are doing something like this, that you probably just should've flattened your data structure or something. However, you are using other properties within the template to dynamically alter the way each column is displayed. I can see now why you want the new BindableProperty, although it is a very specialized use-case. Looks like other data grids do support this. Sometimes they call it |
|
@copilot resolve the merge conflicts in this pull request |
Allow binding on the row object, but set sort based on a specific attribute, for use in control templates