【问题标题】:How to Group Listview items that are added through XAML?如何对通过 XAML 添加的 Listview 项目进行分组?
【发布时间】:2021-11-15 18:07:35
【问题描述】:

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

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

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

【问题讨论】:

  • 什么是分组标准。
  • @Rekshino 我只是想在一个名为 Fruits and Tomato 的组下有一个像 Apple 和 Orange 的分组视图,以及在一个名为蔬菜的组下的 Potato。我这里没有分组标准。
  • 链接你如何在How do I group items in a WPF ListView后面的代码中做到这一点。

标签: wpf xaml listview grouping groupstyle


【解决方案1】:

我会说如果你想在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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多