Binding SortOrder in PropertyGroupDescriptor in CollectionView
Environment
| Version | Product | Author |
|---|---|---|
| 13.0.0 | Telerik UI for .NET MAUI CollectionView | Dobrinka Yordanova |
Description
I attempted to bind the SortOrder property in the PropertyGroupDescriptor in a grouped CollectionView, but encountered the error:
No property, BindableProperty, or event found for "SortOrder", or mismatching type between value and property.
This issue arises because the SortOrder property is not bindable. I need a solution to dynamically manage the sort order.
This knowledge base article also answers the following questions:
- How can I implement dynamic sorting in CollectionView?
- How do I use attached properties for
PropertyGroupDescriptorin .NET MAUI? - How can I change the sort order programmatically in CollectionView?
Solution
Use one of the following approaches to manage the SortOrder dynamically:
Approach 1: Managing GroupDescriptors Collection
Clear and re-add the GroupDescriptors collection with the desired SortOrder:
this.collectionview.GroupDescriptors.Clear();
this.collectionview.GroupDescriptors.Add(new PropertyGroupDescriptor
{
PropertyName = nameof(ReceiptModel.TransactionDateMonth),
SortOrder = SortOrder.Descending
});
Approach 2: Using Attached Property
Define an attached property on the CollectionView level to bind and update the SortOrder. Follow these steps:
1. Create a static class to define the attached property:
public static class TelerikUtils
{
public static readonly BindableProperty MySortOrderProperty =
BindableProperty.CreateAttached("MySortOrder", typeof(SortOrder), typeof(TelerikUtils), SortOrder.Ascending, propertyChanged: OnMySortOrderChanged);
public static SortOrder GetMySortOrder(BindableObject bindable)
{
return (SortOrder)bindable.GetValue(MySortOrderProperty);
}
public static void SetMySortOrder(BindableObject bindable, SortOrder value)
{
bindable.SetValue(MySortOrderProperty, value);
}
private static void OnMySortOrderChanged(BindableObject bindable, object oldValue, object newValue)
{
RadCollectionView cv = (RadCollectionView)bindable;
var descriptor = cv.GroupDescriptors.FirstOrDefault();
SortOrder mySortOrder = (SortOrder)newValue;
descriptor.SortOrder = mySortOrder;
}
}
2. Bind the SortOrder property to the ViewModel using the attached property in XAML:
<telerik:RadCollectionView local:TelerikUtils.MySortOrder="{Binding SortingOrder}" ...>
<telerik:RadCollectionView.GroupDescriptors>
<telerik:PropertyGroupDescriptor PropertyName="TransactionDateMonth" />
</telerik:RadCollectionView.GroupDescriptors>
</telerik:RadCollectionView>
3. Create a ViewModel with a property for SortingOrder:
public class MainPageViewModel : NotifyPropertyChangedBase
{
private SortOrder sortingOrder = SortOrder.Ascending;
public SortOrder SortingOrder
{
get => sortingOrder;
set
{
if (sortingOrder != value)
{
sortingOrder = value;
OnPropertyChanged();
}
}
}
}
4. For example, on a button or another UI element modify the SortingOrder:
private void Button_Clicked(object sender, EventArgs e)
{
ViewModel.SortingOrder = ViewModel.SortingOrder == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
}