WPF DataGrid使用sums和其他字段进行分组
我有一个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"); } }
C#;
ListCollectionView groupcollection = new ListCollectionView(myCollection); groupcollection.GroupDescriptions.Add(new PropertyGroupDescription("ID")); myDataGrid.ItemsSource = groupcollection;
XAML:
这完全有效但现在在Expander.Header
我想添加一个“Amount”和“Descript”值的摘要。 因此,例如,如果集合中有3条记录,ID为“ABC”,每条记录为20,而ABC的描述为“我的计数”,我希望看到;
ABC My Count total 60
我该怎么办?
您可以使用传递组头的Items属性的转换器,例如
转换器执行计算并将总数作为文本块的字符串传回:
public class GroupsToTotalConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is ReadOnlyObservableCollection
至于描述,我建议也按此分组,并编写另一个转换器,以类似于上面的方式从Items中提取描述。