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对象的集合(它作为项目源绑定到listview),我想基于这些ClassA对象的一个属性对项目进行分组。我肯定我错过了一些东西。但是我不知道。
2 个解决方案
#1
0
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.
我建议在ViewModel IObservableCollection
<ListView ItemsSource="{Binding Path=Properties}">
#2
0
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- 4a82b - 226ed56ac90c/grouping-listview-wpf?
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:
Xaml:
<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>
#1
0
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.
我建议在ViewModel IObservableCollection
<ListView ItemsSource="{Binding Path=Properties}">
#2
0
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- 4a82b - 226ed56ac90c/grouping-listview-wpf?
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:
Xaml:
<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>