在 ListView 中显示分组项目的总和
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了扩展 e.tadeu 所说的内容,您可以将 HeaderTemplate 的 DataTemplate 绑定到 CollectionViewGroup 的 Items 属性。这将返回当前组中的所有项目。
然后,您可以提供一个转换器,该转换器将从该项目集合中返回所需的数据。就您而言,您说您想要小时数的总和。您可以实现一个转换器,执行以下操作:
然后您可以在数据模板上使用此转换器:
干杯!
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:
Then you could use this converter on your data template:
Cheers!
使用转换器。
请参阅:
http://www.codeproject.com/KB/WPF/WPFAggregateConverter.aspx< /a>
这也可能对您有帮助:
http://www.codeproject.com/KB /WPF/DsxGridCtrl.aspx
Use a Converter.
See:
http://www.codeproject.com/KB/WPF/WPFAggregateConverter.aspx
This might help you too:
http://www.codeproject.com/KB/WPF/DsxGridCtrl.aspx
只需使用 GroupStyle 中的“ItemCount”即可显示当前包含项目的数量,按照此教程关于ListView分组。
Just use "ItemCount" in GroupStyle to show current count of containing items, per this tutorial on ListView grouping.