Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

c# - WPF DataGrid Grouping with sums and other fields

I have a DataGrid that is bound to collection and that I want to be grouped. 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"); }
  }

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>

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 

How would I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...