Question

I have an instance of ListCollectionView which is grouped using PropertyGroupDescription as shown below

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:

<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>
Was it helpful?

Solution

Just use the ItemCount property of the 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top