8

我正在使用 MVVM 设计模式创建一个 WPF TimeCard 应用程序,并且我试图显示用户每天打卡的总时间(总)。我有一个 ListView,其中所有 TimeCard 数据使用以下 XAML 分组:

<ListView.GroupStyle>
    <GroupStyle ContainerStyle="{StaticResource GroupItemStyle}">
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Name, StringFormat=\{0:D\}}" FontWeight="Bold"/>
                    <TextBlock Text="  (" FontWeight="Bold"/>
                    <!-- This needs to display the sum of the hours -->
                    <TextBlock Text="{Binding ???}" FontWeight="Bold"/>
                    <TextBlock Text=" hours)" FontWeight="Bold"/>
                </StackPanel>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
</ListView.GroupStyle>

这甚至可能吗?起初我以为我会创建一个 CollectionViewGroup 的部分类并添加我自己的属性。但我不确定这是否会奏效。也许有更好的解决方案......有什么建议吗?

4

3 回答 3

19

要扩展 e.tadeu 所说的内容,您可以将 HeaderTemplate 的 DataTemplate 绑定到 CollectionViewGroup 的 Items 属性。这将返回当前组中的所有项目。

然后,您可以提供一个转换器,该转换器将从该项目集合中返回您所需的数据。在你的情况下,你说你想要小时的总和。您可以实现一个转换器,它执行以下操作:

public class GroupHoursConverter : IValueConverter
{

    public object Convert(object value, System.Type targetType, 
                          object parameter, 
                          System.Globalization.CultureInfo culture)
    {
        if (null == value)
            return "null";

        ReadOnlyObservableCollection<object> items = 
              (ReadOnlyObservableCollection<object>)value;

        var hours = (from i in items
                     select ((TimeCard)i).Hours).Sum();

        return "Total Hours: " + hours.ToString();
    }

    public object ConvertBack(object value, System.Type targetType, 
                              object parameter, 
                              System.Globalization.CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}

然后你可以在你的数据模板上使用这个转换器:

    <Window.Resources>
        <local:GroupHoursConverter  x:Key="myConverter" />
    </Window.Resources>

    <ListView.GroupStyle>
        <GroupStyle ContainerStyle="{StaticResource GroupItemStyle}">
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name, 
                                                  StringFormat=\{0:D\}}" 
                                   FontWeight="Bold"/>
                        <TextBlock Text="  (" FontWeight="Bold"/>
                        <!-- This needs to display the sum of the hours -->
                        <TextBlock Text="{Binding Path=Items, 
                                         Converter={StaticResource myConverter}}"
                                   FontWeight="Bold"/>
                        <TextBlock Text=" hours)" FontWeight="Bold"/>
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListView.GroupStyle>

干杯!

于 2010-02-10T16:47:44.150 回答
1

使用转换器。

看:

http://www.codeproject.com/KB/WPF/WPFAggregateConverter.aspx

这也可能对您有所帮助:

http://www.codeproject.com/KB/WPF/DsxGridCtrl.aspx

于 2010-02-10T15:01:54.283 回答
-2

根据 ListView 分组的本教程,只需在 GroupStyle 中使用“ItemCount”即可显示包含项目的当前计数。

于 2015-10-28T12:21:36.137 回答