WPF DataGrid使用sums和其他字段进行分组

Xap*_*ann 7 c# wpf xaml datagrid

我有一个DataGrid绑定到集合,我想分组.这是代码

采集:

private string _ID;
private string _Descript;
private decimal _Amount;
public string ID
{
   get { return _ID; }
   set { _ID = value; NotifyPropertyChanged("ID"); }
 }
 public decimal Amount
 {
   get { return _Amount; }
   set { _Amount = value; NotifyPropertyChanged("Amount"); }
 }
 public string Descript
 {
   get { return _Descript; }
   set { _Descript = value; NotifyPropertyChanged("Descript"); }
  }
Run Code Online (Sandbox Code Playgroud)

C#;

ListCollectionView groupcollection = new   ListCollectionView(myCollection);
groupcollection.GroupDescriptions.Add(new PropertyGroupDescription("ID"));
myDataGrid.ItemsSource = groupcollection;
Run Code Online (Sandbox Code Playgroud)

XAML:

<DataGrid Name="myDataGrid">
<DataGrid.GroupStyle>
    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Name}" />
                </StackPanel>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander>
                                <Expander.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Path=Name}" Margin="5"/>
                                        <TextBlock Text="Count" Margin="5" />
                                        <TextBlock Text="{Binding Path=ItemCount}" Margin="5"/>
                                    </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</DataGrid.GroupStyle>
Run Code Online (Sandbox Code Playgroud)

这完美有效,但现在Expander.Header我想添加一个"金额"和"描述"值的摘要.因此,例如,如果集合中有3条记录,ID为"ABC",每条记录为20,而ABC的描述为"我的计数",我希望看到;

ABC My Count total 60 
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

小智 15

您可以使用传递组头的Items属性的转换器,例如

<Window.Resources>
    <local:GroupsToTotalConverter x:Key="groupsConverter" />
</Window.Resources>

<Expander.Header>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=Name}" Margin="5"/>
        <TextBlock Text="total" Margin="5" />
        <TextBlock Text="{Binding Path=Items, Converter={StaticResource groupsConverter}}" Margin="5" />
    </StackPanel>
Run Code Online (Sandbox Code Playgroud)

转换器执行计算并将总数作为文本块的字符串传回:

public class GroupsToTotalConverter : 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)
            {
                total += gi.Amount;
            }
            return total.ToString();
        }
        return "";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}
Run Code Online (Sandbox Code Playgroud)

至于描述,我建议也按此分组,并编写另一个转换器,以类似于上面的方式从Items中提取描述.