在 ListView 中显示分组项目的总和

发布于 2024-08-20 03:02:55 字数 1024 浏览 4 评论 0原文

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

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

I'm creating a WPF TimeCard app using the MVVM design pattern, and I'm trying to display the sum (total) hours the user has clocked in grouped by each day. I have a ListView with all of the TimeCard data broken into groups using the following 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>

Is this even possible? At first I thought I would create a partial class of the CollectionViewGroup and add my own properties. But I'm not sure this will even work. Perhaps there is a better solution... any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

删除会话 2024-08-27 03:02:55

为了扩展 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>

干杯!

To expand on what e.tadeu said, you can bind your HeaderTemplate's DataTemplate to the Items property of the CollectionViewGroup. This will return you all of the items that are in the current group.

Then you can supply a converter that will return you the desired data from that collection of items. In your case, you say you want the sum of the hours. You could implement a converter that does something like:

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();
    }
}

Then you could use this converter on your data template:

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

Cheers!

鯉魚旗 2024-08-27 03:02:55

只需使用 GroupStyle 中的“ItemCount”即可显示当前包含项目的数量,按照此教程关于ListView分组

Just use "ItemCount" in GroupStyle to show current count of containing items, per this tutorial on ListView grouping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文