Wpf ListCollectionView-GroupDescriptions-如何获取分组项目计数?
我有一个Wpf ListCollectionView-GroupDescriptions-如何获取分组项目计数?,wpf,datagrid,listcollectionview,Wpf,Datagrid,Listcollectionview,我有一个ListCollectionView的实例,它使用PropertyGroupDescription进行分组,如下所示 ListCollectionView view = new ListCollectionView(calls); view.GroupDescriptions.Add(new PropertyGroupDescription("Technician.Name")); DGCalls.ItemsSource = view; 分组工作正常,但我想显示分组项目的计数,如下所示
ListCollectionView
的实例,它使用PropertyGroupDescription
进行分组,如下所示
ListCollectionView view = new ListCollectionView(calls);
view.GroupDescriptions.Add(new PropertyGroupDescription("Technician.Name"));
DGCalls.ItemsSource = view;
分组工作正常,但我想显示分组项目的计数,如下所示
可以显示计数吗
下面是我创建的示例应用程序
代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Technician tech1 = new Technician() { Name = "Tech01" };
Technician tech2 = new Technician() { Name = "Tech02" };
List<ServiceCall> calls = new List<ServiceCall>();
calls.Add(new ServiceCall() { ID = 1, Technician = tech1});
calls.Add(new ServiceCall() { ID = 2, Technician = tech1 });
calls.Add(new ServiceCall() { ID = 3, Technician = tech1 });
calls.Add(new ServiceCall() { ID = 4, Technician = tech2 });
calls.Add(new ServiceCall() { ID = 5, Technician = tech2 });
ListCollectionView view = new ListCollectionView(calls);
view.GroupDescriptions.Add(new PropertyGroupDescription("Technician.Name"));
DGCalls.ItemsSource = view;
}
}
public class ServiceCall
{
public int ID { get; set; }
public Technician Technician { get; set; }
}
public class Technician
{
public String Name { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication1
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
技师tech1=新技师(){Name=“Tech01”};
技师tech2=新技师(){Name=“Tech02”};
列表调用=新列表();
Add(new ServiceCall(){ID=1,technology=tech1});
Add(new ServiceCall(){ID=2,technology=tech1});
Add(new ServiceCall(){ID=3,technology=tech1});
Add(new ServiceCall(){ID=4,technology=tech2});
Add(新的ServiceCall(){ID=5,technology=tech2});
ListCollectionView=新建ListCollectionView(调用);
view.groupdescription.Add(新属性groupdescription(“technology.Name”);
DGCalls.ItemsSource=视图;
}
}
公共类服务呼叫
{
公共int ID{get;set;}
公共技术员{get;set;}
}
公营技术员
{
公共字符串名称{get;set;}
}
}
XAML:
只需使用CollectionViewSource的属性:
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Background="Blue" >
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Foreground="White" FontWeight="Bold"/>
<TextBlock Text="{Binding ItemCount}" Foreground="White" FontWeight="Bold"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Background="Blue" >
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Foreground="White" FontWeight="Bold"/>
<TextBlock Text="{Binding ItemCount}" Foreground="White" FontWeight="Bold"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>