WPF DataGrid 分组与总和和其他字段 [英] WPF DataGrid Grouping with sums and other fields

查看:32
本文介绍了WPF DataGrid 分组与总和和其他字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到集合的 DataGrid,我想对其进行分组.这是代码

I have a DataGrid that is bound to collection and that I want to be grouped. Here is the code

集合:

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

C#;

ListCollectionView groupcollection = new   ListCollectionView(myCollection);
groupcollection.GroupDescriptions.Add(new PropertyGroupDescription("ID"));
myDataGrid.ItemsSource = groupcollection;

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>

这很完美,但现在在 Expander.Header 中,我想添加金额"和描述"值的摘要.例如,如果集合中有 3 条记录,ID 为ABC",每条记录为 20,而 ABC 的描述为My Count",我想查看;

This works perfectly but now in the Expander.Header I want to added a summary of a "Amount" and "Descript" value. So for example if there were 3 records in the collection with ID "ABC" each one being 20 and the description for ABC being "My Count" I would want to see;

ABC My Count total 60 

我该怎么做?

推荐答案

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

You could use a converter that's passed the Items property of the group header e.g.

<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>

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

where the converter performs the calculation and passes back the total as the string for the text block:

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

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

As for the description I'd suggest also grouping by that, and writing another converter to pull out the description from the Items in a similar manner to above.

这篇关于WPF DataGrid 分组与总和和其他字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆