Back to results for “Collections”
.NET MAUI › Advanced Data Binding & Collections

How do you implement grouping in a CollectionView?

Advanced

Answer

Set IsGrouped="True" and bind ItemsSource to a collection of groups (each an IEnumerable, often a class deriving from List<T> with a key). Provide GroupHeaderTemplate for headers. The view model shapes flat data into grouped structures, typically with LINQ GroupBy.
<CollectionView ItemsSource="{Binding Groups}" IsGrouped="True">
    <CollectionView.GroupHeaderTemplate>
        <DataTemplate><Label Text="{Binding Key}" FontAttributes="Bold" /></DataTemplate>
    </CollectionView.GroupHeaderTemplate>
</CollectionView>
Real-world example A contacts list groups people under alphabetical headers built with LINQ GroupBy in the view model.

Common follow-ups: How is grouped data shaped? | What template renders headers?