ListCollectionView - GroupDescriptions - 如何计算分组项?

时间:2013-03-25 12:36:59

标签: wpf datagrid listcollectionview

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

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

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

enter image description here

是否可以显示计数?

以下是我创建的示例应用。

代码背后:

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>

1 个答案:

答案 0 :(得分: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>