我有一个绑定到集合的DataGrid,我想对其进行分组。这是代码:
集合:
C#;
我该怎么做?
集合:
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”,那么我想看到的是;ABC My Count total 60
我该怎么做?