0

我在 XAML 中有一个 Listview,其中只有很少的 Listview 项是通过 XAML 添加的,而我的 listview 没有其他 Items 源。我想将这些项目分为两组。我的 Listview 代码是这样的

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

我想把它们分成两组。这可能吗。

4

1 回答 1

0

我会说,如果您想将项目分组,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>
于 2021-09-22T15:02:22.670 回答