0

I have a Listview in XAML with few Listview Items that are added through XAML only and my listview has no other Items source.I want to group the items into two group.My Listview code is something like this

<ListView Name="List">
 <ListViewItem Content="Apple" />
 <ListViewItem Content="Orange" />
 <ListViewItem Content="Tomato" />
 <ListViewItem Content="Potato" />
<ListView />

I want to group them into two sets .Is this possible.

Rekshino
  • 6,954
  • 2
  • 19
  • 44
NovemberMan
  • 65
  • 1
  • 4
  • What is grouping criteria. – Rekshino Sep 22 '21 at 10:30
  • @Rekshino I am looking just to have a grouped view Like Apple and Orange under a group called Fruits and Tomato and Potato under a group called Vegetables. I am not having a grouping criteria here. – NovemberMan Sep 22 '21 at 13:49
  • Link how you can do it in code behind [How do I group items in a WPF ListView](https://stackoverflow.com/q/639809/7713750). – Rekshino Sep 22 '21 at 15:33

1 Answers1

0

I would say if you want to group items in ListView you will not go around the CollectionViewSource. It's easier to fill it in ViewModel, but if you haven't VM you can also create one in XAML.

<StackPanel>
    <StackPanel.Resources>
            <x:Array x:Key="arr" Type="{x:Type ListViewItem}">
                <ListViewItem Content="Orange" Tag="Meal"/>
                <ListViewItem Content="Apple" Tag="Meal"/>
                <ListViewItem Content="Cat" Tag="Pets"/>
                <ListViewItem Content="Dog" Tag="Pets"/>
                <ListViewItem Content="Fish" Tag="Diverse"/>
                <ListViewItem Content="Duck" Tag="Diverse"/>
            </x:Array>
            <CollectionViewSource x:Key="CVS" Source="{DynamicResource arr}">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="Tag" />
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
    </StackPanel.Resources>
    <ListView ItemsSource="{Binding Source={StaticResource CVS}}">
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Name}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>
</StackPanel>
Rekshino
  • 6,954
  • 2
  • 19
  • 44