集团统计(Group statistic)

2019-10-21 05:58发布

我想要DataGrid中包含分组

我的问题是如何做到这一点“平均值”,在组头?

下面是一个巨大的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>

产生这种输出

我可以输出组名(绿色箭头)和计数器(红色箭头)。

如何为任一列输出平均值(让它成为A ),而不是“测试”?

Answer 1:

您将需要一个转换器将转换Items属性为平均值。 事情是这样的:

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

我想,项目类型为Item 。 您使用转换器在你的XAML代码,也许你需要一些StringFormat格式化值表示。

假设你有这个资源的地方进行访问:

<local:ItemsToAverageConverter x:Key="avgConverter"/>

该代码应该改变这样的:

<TextBlock Grid.Column="1" Text="{Binding Items,
                                  Converter={StaticResource avgConverter},
                                  ConverterParameter=A,
                                  StringFormat=f2}" Margin="10,0,0,0"/>


文章来源: Group statistic