要扩展e.tadeu所说的内容,可以将headerTemplate的数据模板绑定到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>
干杯!