如何对 WPF ListView 中的项目进行分组

编程入门 行业动态 更新时间:2024-10-26 23:35:00
本文介绍了如何对 WPF ListView 中的项目进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 ListView,我想将结果分组到其中,但是我发现的示例不起作用.如何对结果进行分组?

I have a ListView that I want to group results into, however the examples I am finding are not working. How can I group my results?

我想对自定义对象的 Status 属性进行分组.

I want to group on the Status property of a custom object.

这就是我所拥有的:

<ListView IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="Transparent" SelectionChanged="ListView_SelectionChanged" Name="lstShelvedOrders"> <ListView.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <TextBlock FontWeight="Bold" FontSize="15" Text="{Binding Path=Status}"/> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </ListView.GroupStyle> <ListView.ItemContainerStyle> <Style TargetType="{x:Type ListViewItem}"> <Setter Property="BorderThickness" Value="1" /> <Setter Property="Width" Value="Auto" /> <Setter Property="FontSize" Value="10.4" /> </Style> </ListView.ItemContainerStyle> <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding Path=Number}" Header="Shelve ID" /> <GridViewColumn DisplayMemberBinding="{Binding Path=Customer}" Header="Customer" /> <GridViewColumn DisplayMemberBinding="{Binding Path=PurchaseOrderNo}" Header="PO Number" /> <GridViewColumn DisplayMemberBinding="{Binding Path=SubmittedBy}" Header="Shelved By" /> <GridViewColumn DisplayMemberBinding="{Binding Path=OrderDate, StringFormat=MMM dd, yyyy}" Header="Date" /> <GridViewColumn DisplayMemberBinding="{Binding Path=CustomerTerms.Description}" Header="Order Terms" /> <GridViewColumn DisplayMemberBinding="{Binding Path=ShippingMethod.Description}" Header="Shipping" /> <GridViewColumn DisplayMemberBinding="{Binding Path=TotalPrice, StringFormat=c}" Header="Order Total" /> </GridView> </ListView.View> </ListView>

这是我拥有的代码:

void ShelvedOrderList_DataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e) { AddGrouping(); } private void AddGrouping() { if ( lstShelvedOrders.ItemsSource == null) { return; } CollectionView myView = (CollectionView)CollectionViewSource.GetDefaultView(lstShelvedOrders.ItemsSource); PropertyGroupDescription groupDescription = new PropertyGroupDescription("Status"); myView.GroupDescriptions.Add(groupDescription); }

推荐答案

我马上注意到一件事 - GroupStyle.HeaderTemplate 将应用于 CollectionViewGroup, 所以你的 DataTemplate 应该是这样的:

I notice one thing right away - the GroupStyle.HeaderTemplate will be applied to a CollectionViewGroup, so your DataTemplate should probably look like this:

<GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <TextBlock FontSize="15" FontWeight="Bold" Text="{Binding Name}"/> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle>

CollectionViewGroup.Name 将为该组分配 Status 的值.

CollectionViewGroup.Name will be assigned the value of Status for that group.

更多推荐

如何对 WPF ListView 中的项目进行分组

本文发布于:2023-11-25 16:30:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   WPF   ListView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!