How to create a grouped ListView in .NET MAUI (SfListView) ?
The .NET MAUI ListView (SfListView) supports grouping items based on custom logic using the GroupDescriptor. This guide demonstrates how to implement grouping based on the first character of each item's name.
Key Properties of GroupDescriptor
The GroupDescriptor object holds the following properties:
- PropertyName: Identifies the name of the property to group by.
- KeySelector : Function defining custom logic to determine the group key.
- Comparer : Optional custom comparer for sorting grouped items.
C#
Below is an example of creating a grouped ListView based on the first character of the contact name.
public class Behavior : Behavior<Syncfusion.Maui.ListView.SfListView>
{
private Syncfusion.Maui.ListView.SfListView ListView;
protected override void OnAttachedTo(Syncfusion.Maui.ListView.SfListView bindable)
{
ListView = bindable;
ListView.DataSource.GroupDescriptors.Add(new GroupDescriptor()
{
PropertyName = "ContactName",
KeySelector = (object obj1) =>
{
var item = (obj1 as ListViewContactInfo);
return item.ContactName[0].ToString();
},
});
base.OnAttachedTo(bindable);
}
protected override void OnDetachingFrom(Syncfusion.Maui.ListView.SfListView bindable)
{
ListView = null;
base.OnDetachingFrom(bindable);
}
}
Output
Download the complete sample on GitHub
Conclusion
I hope you enjoyed learning how to create a grouped ListView in .NET MAUI.
You can refer to our .NET MAUI ListView feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. Explore our .NET MAUI ListView example to understand how to create and manipulate data.