Category / Section
How to create a grouped ListView in .NET MAUI (SfListView) ?
1 min read
You can create a grouped ListView in .NET MAUI by creating a GroupDescriptor object and inserting it into the GroupDescriptors collection and .NET MAUI ListView (SfListView) supports grouping items based on custom logic for each GroupDescriptor via KeySelector.
The GroupDescriptor object holds the following properties:
- PropertyName: Describes the name of the property to be grouped.
- KeySelector : Describes selector to return the group key.
- Comparer : Describes comparer to be applied when sorting takes place.
C#
The below code snippet explains how to group the items based on first character of the item.
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); } }
Take a moment to peruse the documentation, to learn more about grouping in SfListView with code.