CollectionViewSource + PropertyGroupDescription - 组中项目的数量

时间:2010-11-01 10:47:01

标签: wpf grouping collectionviewsource

在我的WPF应用程序中,我有一个CollectionViewSource,它提供了一个私有ObservableCollection的视图。 CollectionViewSource有一个PropertyGroupDescription,它在ListBox中用于向用户的首选项提供数据。

在ListBox GroupStyle中使用包含Expander Control的ControlTemplate,结果非常好。但是,除了Group Name之外,我想在Expander Header中显示每个组中的项目数。关于绑定路径的任何想法?

此致 利安

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

2 个答案:

答案 0 :(得分:8)

您必须使用 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>

答案 1 :(得分:3)

我更新了Group Style,如下所示。我发现我无法在Expander.Header属性中使用MultiBinding,屏幕上没有显示任何内容,我需要包含一个中间TextBlock。

<Style x:Key="basicGroupStyle" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                    <Expander IsExpanded="True">
                        <Expander.Header>
                            <TextBlock>
                                <TextBlock.Text>
                                    <MultiBinding StringFormat="{}{0} ({1})">
                                        <Binding Path="Name"/>
                                        <Binding Path="ItemCount"/>
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>
                        </Expander.Header>

                        <ItemsPresenter/>

                    </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>