In my collectionview on .Net Maui Im trying to have Categories, then items per category. This is trivial with viewmodel and Linq, List, or even array indexes. such as in Blazor
foreach(var cat in viewModel.Categories)
{
<select id="@cat" [email protected](z=> z.Category == cat)>
</select>
}
But im having trouble doing this with CollectionView
<CollectionView x:Name="collectionViewMenuPerCategory" ItemsSource="{Binding Categories}" ItemsLayout="VerticalList" Margin="10" >
<CollectionView.ItemTemplate>
<DataTemplate>
<VerticalStackLayout>
<Label text="{Binding CategoryName}" FontSize="34">
<CollectionView x:Name="CollectionViewPerCategory" ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type local:MenuViewModel}}, Path=Menu.Items.Where(z=> z.Category == @CategoryName?}" ItemsLayout="VerticalList">
<!-- Each Item in sub collectionview for each category -->
</CollectionView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
I have see ICollectionView filtering for WPF but dont see for Maui
Yes, CollectionView
supports displaying grouped data, and defines the following properties that control how it will be presented:
bool
, indicates whether the underlying data should be displayed in groups. The default value of this property is
false.DataTemplate
, the template to use for the header of each group.DataTemplate
, the template to use for the footer of each group.These properties are backed by BindableProperty
objects, which means that the properties can be targets of data bindings.
The process for grouping data, therefore, is to:
For more information, you can check document: Display grouped data in a CollectionView.