ListView WPF 中的项目分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37657968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:50:01  来源:igfitidea点击:

Grouping of items in ListView WPF

wpflistviewuwpgroupingcollectionviewsource

提问by tavier

I have a ListView where I want to group the items based on a field of the item object. Below is the code I have:

我有一个 ListView,我想根据项目对象的字段对项目进行分组。下面是我的代码:

<ListView ItemsSource="{x:Bind MyVM.CollectionOfClassA, Mode=OneWay}"
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock FontSize="15" FontWeight="Bold" Text="{Binding DateTimePropertyOfClassA}"/>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

Is there something I am missing? I want to group the items based on the DateTime property of ClassA objects. Also, if there is no item for any particular day, I would still like to show that date with empty list under that group (for that day). How can I achieve it?

有什么我想念的吗?我想根据 ClassA 对象的 DateTime 属性对项目进行分组。此外,如果任何特定日期都没有项目,我仍然希望在该组下显示该日期并带有空列表(当天)。我怎样才能实现它?

Edit: I am not able to use CollectionViewSource since my VM contains the collection of ClassA object (which is bound as item source to the listview) and I want to group the items based on one property of those ClassA objects. I am sure there is something I am missing out on. But I am not able to figure it out.

编辑:我无法使用 CollectionViewSource,因为我的 VM 包含 ClassA 对象的集合(它作为项目源绑定到列表视图),我想根据这些 ClassA 对象的一个​​属性对项目进行分组。我确定我错过了一些东西。但我无法弄清楚。

采纳答案by tavier

Here is the solution for anyone looking for it (thanks to the WPF masters out there https://social.msdn.microsoft.com/Forums/windowsapps/en-US/812ed260-e113-4a8b-9322-226ed56ac90c/grouping-of-items-in-listview-wpf?forum=wpdevelop&prof=required):

这是任何寻找它的人的解决方案(感谢那里的 WPF 大师https://social.msdn.microsoft.com/Forums/windowsapps/en-US/812ed260-e113-4a8b-9322-226ed56ac90c/grouping-of -items-in-listview-wpf?forum=wpdevelop&prof=required):

public class ClassA
{
    public DateTime DateTimePropertyOfClassA { get; set; }
}

public class MyVM
{
    public MyVM()
    {
        //return a grouped collection:
        Grouped = from x in CollectionOfClassA group x by x.DateTimePropertyOfClassA into grp orderby grp.Key select grp;
    }

    public IList<ClassA> CollectionOfClassA { get; set; } = new List<ClassA>()
    {
            new ClassA(){ DateTimePropertyOfClassA = DateTime.Parse("2016-01-01")},
            new ClassA(){ DateTimePropertyOfClassA = DateTime.Parse("2016-03-01")},
            new ClassA(){ DateTimePropertyOfClassA = DateTime.Parse("2016-03-01")},
            new ClassA(){ DateTimePropertyOfClassA = DateTime.Parse("2016-03-01")},
            new ClassA(){ DateTimePropertyOfClassA = DateTime.Parse("2016-03-01")},
            new ClassA(){ DateTimePropertyOfClassA =DateTime.Parse("2016-06-01")}
    };

    public IEnumerable<object> Grouped { get; }
}

Xaml:

Xml:

<Page.Resources>
        <CollectionViewSource x:Name="cvs"
                              IsSourceGrouped="True"
                              Source="{x:Bind MyVM.Grouped, Mode=OneWay}"/>
</Page.Resources>

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemsSource="{Binding Source={StaticResource cvs}}">
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock FontSize="15" FontWeight="Bold" Text="{Binding Key}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>
</StackPanel>

回答by Petar Stojanovski

I suggest creating a collection in the ViewModel IObservableCollection<ClassA>Properties (or whatever name), add it in the MainWindow class and then simply bind it in the ListView.

我建议在 ViewModelIObservableCollection<ClassA>属性(或任何名称)中创建一个集合,将其添加到 MainWindow 类中,然后简单地将其绑定到 ListView 中。

<ListView ItemsSource="{Binding Path=Properties}">