我会说,如果您想将项目分组,ListView
则不会绕过CollectionViewSource
. 在 ViewModel 中填充它更容易,但如果您没有 VM,您也可以在 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>