【问题标题】:Display sum of grouped items in ListView在 ListView 中显示分组项目的总和
【发布时间】:2010-02-06 04:11:46
【问题描述】:

我正在使用 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 的部分类并添加我自己的属性。但我不确定这是否会奏效。也许有更好的解决方案...有什么建议吗?

【问题讨论】:

    标签: wpf listview mvvm grouping listcollectionview


    【解决方案1】:

    要扩展 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>
    

    干杯!

    【讨论】:

    • 谢谢!我总是忘记你可以用 ValueConverters 做的所有事情。感谢您提供示例代码。
    【解决方案2】:

    【讨论】:

    • 谢谢...虽然您给出的答案也是正确的,但我还是决定将其交给 NathanAW,因为他提供的代码直接适合我的解决方案。
    【解决方案3】:

    根据tutorial on ListView grouping,只需在 GroupStyle 中使用“ItemCount”即可显示包含项目的当前计数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 2020-06-20
      相关资源
      最近更新 更多