Wpf – CollectionViewSource+PropertyGroupDescription – count of items in a group

collectionviewsourcegroupingwpf

In my WPF application I have a CollectionViewSource which is providing a view to a private ObservableCollection. The CollectionViewSource has a PropertyGroupDescription which is utilised in a ListBox to present data to the User's preference.

Using a ControlTemplate containing a Expander Control within the ListBox GroupStyle, the result is quite nice. However, I would like to show the number of items in each group in the Expander Header in addition to the Group Name. Any ideas on the binding path?

Regards,
Liam

<Style x:Key="basicGroupStyle" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <Expander Header="{Binding Name}" IsExpanded="True">
                    <ItemsPresenter/>
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ListBox ItemsSource="{Binding Source={StaticResource myViewSource}}">
    <ListBox.GroupStyle>
         <GroupStyle ContainerStyle="{StaticResource basicGroupStyle}"/>
    </ListBox.GroupStyle>
</ListBox>

Best Answer

you have to use property ItemCount

<Window x:Class="WpfApplication11.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <XmlDataProvider x:Key="data">
            <x:XData>
                <Animals xmlns="">
                    <Animal name="Dory" Species="Fish" />
                    <Animal name="Felix" Species="Cat" />
                    <Animal name="Fluffy" Species="Dog" />
                    <Animal name="Jake" Species="Snake" />
                    <Animal name="Mittens" Species="Cat" />
                    <Animal name="Murtle" Species="Turtle" />
                    <Animal name="Nemo" Species="Fish" />
                    <Animal name="Rex" Species="Dog" />
                    <Animal name="Rover" Species="Dog" />
                    <Animal name="Toonces" Species="Cat" />
                </Animals>
            </x:XData>
        </XmlDataProvider>
        <CollectionViewSource x:Key="animalsBySpecies" Source="{Binding Source={StaticResource data}, XPath=Animals/Animal}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="@Species" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <DockPanel>
        <ScrollViewer DockPanel.Dock="Bottom" VerticalScrollBarVisibility="Auto">
            <ItemsControl ItemsSource="{Binding Source={StaticResource animalsBySpecies}}">
                <ItemsControl.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <GroupBox  >
                                                <GroupBox.Header>
                                                    <Grid>
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition></ColumnDefinition>
                                                            <ColumnDefinition></ColumnDefinition>
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock Text ="{Binding Name}" ></TextBlock>
                                                        <TextBlock Text="(" Grid.Column="1" Margin="15,0,0,0"></TextBlock>
                                                        <TextBlock Text="{Binding ItemCount}" Grid.Column="1" Margin="20,0,0,0"
                                                                   HorizontalAlignment="Right" ></TextBlock>
                                                        <TextBlock Text=")" Margin="0,0,-5,0" Grid.Column="1
                                                                            HorizontalAlignment="Right" ></TextBlock>
                                                    </Grid>
                                                </GroupBox.Header>
                                                    <ItemsPresenter />
                                            </GroupBox>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ItemsControl.GroupStyle>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding XPath=@name}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </DockPanel>
</Window>
Related Topic