有很多Datagrid分组教程和问题,但我似乎无法让我的转换器计算我的项目总数。
情况是我有一个数据网格,显示有时间列的项目,即完成任务的小时数,项目按任务完成日期分组 - 见下文
因此要显示的总数是1/8/16组标题中的7.5
我相信我需要使用Linq使用group属性作为过滤器来查询ObservableCollection,但我很难知道该做什么以及如何做。传递给转换器的集合中的项目似乎只有Group Items而不是下面的项目,就像属于该组的项目不在传递的集合中一样。
任何指针都表示赞赏,如果我错过任何需要的代码,我会清楚地添加它我不太了解这一点 - 提前谢谢
这是我的转换器代码
public class GroupTotalsToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is ReadOnlyObservableCollection<Object>)
{
var items = (ReadOnlyObservableCollection<Object>)value;
Decimal total = 0;
foreach (GroupItem gi in items)
{
//sometype of query to add up timespent
}
return total.ToString();
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
我的Datagrid xmal是
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="271" ItemsSource="{Binding}"
Name="enteredticketsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Width="435"
Background="Black" RowBackground="Black"
AlternatingRowBackground="DarkBlue"
MouseDoubleClick="enteredticketsDataGrid_MouseDoubleClick" Cursor="Arrow"
IsReadOnly="True" ToolTip="Select the Date From and To and Refresh the grid. Dounle click on a return ticket to open the Edit screen for that ticket.">
<DataGrid.Columns>
<DataGridTextColumn x:Name="ticketDateColumn" Binding="{Binding Path=TicketDate, StringFormat=\{0:dd/MM/yy\} }" Header="Date" Width="Auto" />
<DataGridTextColumn x:Name="statusColumn" Binding="{Binding Path=status}" Header="status" Width="SizeToHeader" />
<DataGridTextColumn x:Name="numberColumn" Binding="{Binding Path=number}" Header="number" Width="Auto" />
<DataGridTextColumn x:Name="timespentColumn1" Binding="{Binding Path=timespent}" Header="Time" Width="SizeToHeader" />
<DataGridTextColumn x:Name="subjectColumn" Binding="{Binding Path=subject}" Header="Subject" Width="Auto" />
</DataGrid.Columns>
<DataGrid.GroupStyle>
<!-- Style for groups at top level. -->
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False" Background="#FF112255" BorderBrush="#FF002255" Foreground="#FFEEEEEE" BorderThickness="1,1,1,5">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name, Converter={StaticResource StringToDateTimeConverter }}" Margin="5,0,0,0" Width="100"/>
<TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
<!--<TextBlock FontWeight="Bold" Text="Test"/>-->
<TextBlock Text="{Binding Path=Text, Converter={StaticResource GroupTotalsToStringConverter}}"></TextBlock>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
答案 0 :(得分:1)
试试这个:
XAML
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name, Converter={StaticResource StringToDateTimeConverter }}" Margin="5,0,0,0" Width="100"/>
<TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
<TextBlock Text="{Binding Path=Items, Converter={StaticResource GroupTotalsToStringConverter}, UpdateSourceTrigger=PropertyChanged}" />
</DockPanel>
</Expander.Header>
这会将您的ItemsCollection作为值提供给Converter。特定的数据类型属于您的ItemsSource,可以通过ConverterCall上的调试找到。