组统计 [英] Group statistic
问题描述
我想使 DataGrid
分组如下
I want to make DataGrid
with grouping like below
我的问题是在组头中如何做到平均值?
My question is how to do that "Average value" in group header?
以下是一个巨大的xaml
Below is a huge xaml
<DataGrid x:Name="dataGrid" Grid.Column="1" CanUserResizeRows="False" CanUserSortColumns="False" GridLinesVisibility="None" AutoGenerateColumns="False" ItemsSource="{Binding List}" HeadersVisibility="Column" Grid.IsSharedSizeScope="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding A}" Header="A"/>
<DataGridTextColumn Binding="{Binding B}" Header="B"/>
<DataGridTextColumn Binding="{Binding C}" Header="C"/>
<DataGridTextColumn Binding="{Binding Group}" Header="Group"/>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="GroupItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GroupItem">
<Border BorderBrush="Gray" BorderThickness="0.5" CornerRadius="4" Margin="3">
<Border.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFACACAC" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A"/>
<ColumnDefinition SharedSizeGroup="B"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Margin="10,0,10,0"/>
<TextBlock Grid.Column="1" Text="{Binding ItemCount}" Margin="10,0,0,0"/>
<TextBlock Grid.Column="2" Text="test" Margin="10,0,0,0"/>
</Grid>
<Expander>
<Border BorderBrush="Gray" BorderThickness="0.5" Margin="3">
<ItemsPresenter/>
</Border>
</Expander>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
哪个产生此输出
我可以输出组名(绿色箭头)和计数器(红色箭头)。
I can output group name (green arrow) and counter (red arrow).
如何输出任一列的平均值(让它为 A
)而不是test?
How can I output average value for either column (let it be A
) instead of "test"?
推荐答案
您将需要一个转换器来转换 Items
财产进入平均水平。这样的东西:
You will need a converter to convert the Items
property into the average. Something like this:
public class ItemsToAverageConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture){
var propName = Convert.ToString(parameter);
var items = (IEnumerable<Item>) value;
var prop = typeof(Item).GetProperty(propName);
if(prop == null) return Binding.DoNothing;
return items.Average(i => (double) prop.GetValue(i));
}
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture){
throw new NotImplementedException();
}
}
我认为项目类型是项目
。您在XAML代码中使用该转换器,也许您需要一些 StringFormat
格式化值表示。
I suppose the item type is Item
. You use that converter in your XAML code, maybe you need some StringFormat
to format the value representation.
假设您有资源可以访问:
Suppose you have this resource somewhere accessible:
<local:ItemsToAverageConverter x:Key="avgConverter"/>
代码应该如下更改:
<TextBlock Grid.Column="1" Text="{Binding Items,
Converter={StaticResource avgConverter},
ConverterParameter=A,
StringFormat=f2}" Margin="10,0,0,0"/>
这篇关于组统计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!