How to maintain only one group in the expanded state in .NET MAUI ListView (SfListView) ?
In .NET MAUI ListView (SfListView) you can ensure only one group remains in an expanded state by handling the GroupExpanding event. This approach involves collapsing all other groups when a new group is expanded, maintaining a cleaner and more organized view.
C#
Manually expand the first group in the Loaded event. In the GroupExpanding event, compare the current expanded group with the previous one and collapse the previous expanded group.
public class ListViewBehavior : Behavior<SfListView>
{
private SfListView ListView;
private GroupResult expandedGroup;
protected override void OnAttachedTo(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();
}
});
ListView.Loaded += ListView_Loaded;
ListView.GroupExpanding += ListView_GroupExpanding;
base.OnAttachedTo(bindable);
}
private void ListView_Loaded(object sender, EventArgs e)
{
ListView.CollapseAll();
var group = ListView.DataSource.Groups[0];
ListView.ExpandGroup(group);
}
private void ListView_GroupExpanding(object sender, GroupExpandCollapseChangingEventArgs e)
{
if (e.Groups.Count > 0)
{
var currentGroup = e.Groups[0];
if (expandedGroup != null && expandedGroup.Key != currentGroup.Key)
{
ListView.CollapseGroup(expandedGroup);
}
expandedGroup = currentGroup;
}
}
}
Output
Download the complete sample on GitHub.
Conclusion
I hope you enjoyed learning how to maintain only one group in the expanded state in .NET MAUI ListView.
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.
For current customers, check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the comments section if you have any queries or require clarification. Contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!