wpf ListCollectionView - GroupDescriptions - 如何获得分组的项目数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15615147/
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 08:18:15  来源:igfitidea点击:

ListCollectionView - GroupDescriptions - How to get grouped items count?

wpfdatagridlistcollectionview

提问by Anand Murali

I have an instance of ListCollectionViewwhich is grouped using PropertyGroupDescriptionas shown below

我有一个实例,ListCollectionView使用PropertyGroupDescription如下所示进行分组

ListCollectionView view = new ListCollectionView(calls);
view.GroupDescriptions.Add(new PropertyGroupDescription("Technician.Name"));
DGCalls.ItemsSource = view;

The grouping works fine, but I want to display the count of grouped items as shown below.

分组工作正常,但我想显示分组项目的数量,如下所示。

enter image description here

在此处输入图片说明

Is it possible to display the count?

是否可以显示计数?

Here is the sample app that I created.

这是我创建的示例应用程序。

Code behind:

后面的代码:

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; }
    }

}

XAML:

XAML:

<Grid>
    <Grid.Resources>
        <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 Count}" Foreground="White" FontWeight="Bold"/>
                                </StackPanel>
                            </Expander.Header>
                            <ItemsPresenter/>
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <DataGrid Name="DGCalls" AutoGenerateColumns="False" IsReadOnly="True">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Call ID" Binding="{Binding ID}"/>
            <DataGridTextColumn Header="Technician Name" Binding="{Binding Technician.Name}"/>
        </DataGrid.Columns>
        <DataGrid.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <DataGridRowsPresenter/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
        </DataGrid.GroupStyle>
    </DataGrid>
</Grid>

采纳答案by mathieu

Just use the ItemCountproperty of the CollectionViewSource :

只需使用CollectionViewSource的ItemCount属性:

     <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>