WPF DataGrid Grouping with Sum and Other Fields

I have a DataGrid bound to a collection and I want to group. Here is the code

Collection:

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

WITH#;

 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> 

This works fine, but now in Expander.Header I want to add a summary of the "Sum" and "Descript" values. For example, if the collection had 3 records with the identifier "ABC", each of which was 20, and the description for ABC was "My Count", I would like to see:

 ABC My Count total 60 

How can I do it?

+6
source share
1 answer

You can use the converter that passed the Items property of the group header, for example.

 <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 the final value as a 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 would also suggest grouping it and writing another converter to pull the description from the Elements in the same way above.

+12
source

Source: https://habr.com/ru/post/971887/


All Articles