2

我有一个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; }
    }

}

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

1 回答 1

1

只需使用 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>
于 2013-03-25T12:46:57.140 回答