How to expand or collapse group header of .NET MAUI ListView(SfListView)?
The .NET MAUI ListView (SfListView) supports expanding or collapsing groups when a group header is tapped by setting the AllowGroupExpandCollapse property to True. Additionally, it allows displaying the expanded and collapsed state of the group with an icon by customizing the GroupHeaderTemplate based on the IsExpand property, which determines whether the group is expanded or collapsed.
XAML
Define the GroupHeaderTemplate with an Image to display the icon. Bind the IsExpand property to the Image.Source with converter.
<ListView:SfListView x:Name="listView" ItemSize="70" GroupHeaderSize="50" SelectionMode="Single" IsStickyGroupHeader="True" ItemsSource="{Binding ContactsInfo}" AllowGroupExpandCollapse="True"> <ListView:SfListView.GroupHeaderTemplate> <DataTemplate> <StackLayout Orientation="Horizontal" BackgroundColor="#E4E4E4"> <Image Source="{Binding IsExpand, Converter={StaticResource BoolToImageConverter}}" HeightRequest="30" WidthRequest="30" VerticalOptions="Center" Margin="10,0"/> <Label Text="{Binding Key}" FontSize="22" FontAttributes="Bold" TextColor="#4d4d4d" VerticalOptions="Center" HorizontalOptions="Start"/> </StackLayout> </DataTemplate> </ListView:SfListView.GroupHeaderTemplate> </ListView:SfListView>
C#
The converter returns the appropriate group icon based on the IsExpand property.
public class BoolToImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (bool)value ? "group_expand.png" : "group_collapse.png"; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
Output
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to expand or collapse group headers in the .NET MAUI ListView.
You can refer to our .NET MAUI ListView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also 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 below 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!