How do you implement grouping in a CollectionView?
Short answer
Set IsGrouped="True" and bind ItemsSource to a collection of groups (each an IEnumerable, often a class deriving from List with a key). Provide GroupHeaderTemplate for headers. The view model shapes flat data into grouped structures, typically with LINQ GroupBy.
Advertisement
AModel 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.
Likely follow-ups
- How is grouped data shaped? | What template renders headers?
Related topics