我想实现WPF DataGrid分组。我看到的大多数示例都涉及Observablecollection,并且可以正常工作。但是,我的问题是我正在使用数据集,而我的CollectionViewSource被窃听到了数据集。
我按照下面所附的示例进行了尝试,它似乎可以工作。但是,对于行汇总,转换器仅选择第一个条目,而从第二个条目开始不进行汇总。 我想念什么?我再次不使用Observablecollection,而是使用老式的DataSet。
这是XAML
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1" x:Class="MainWindow"
Title="MainWindow" Height="350" Width="325">
<Window.Resources>
<local:SuTotals x:Key="SuTotals"/>
<local:DataSet1 x:Key="DataSet1"/>
<CollectionViewSource x:Key="SalesViewSource" Source="{Binding Sales, Source={StaticResource DataSet1}}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Description" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<DataGrid x:Name="SalesDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding Source={StaticResource SalesViewSource}}" RowDetailsVisibilityMode="VisibleWhenSelected" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="Column1" IsReadOnly="True" Binding="{Binding Description}" Header="Description" Width="*"/>
<DataGridTextColumn x:Name="Column2" Binding="{Binding Amount, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" Header="Amount" Width="100"/>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="true" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1">
<Expander.Header>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding ElementName=Column1, Path=ActualWidth}"/>
<ColumnDefinition Width="{Binding ElementName=Column2, Path=ActualWidth}"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal">
<TextBlock Grid.Column="0" Text="Total: " FontWeight="Bold" Margin="0,0,15,0" TextAlignment="Right"/>
<TextBlock Grid.Column="1" Text="{Binding Path=ItemCount}" Margin="0,0,15,0" TextAlignment="Right"/>
</StackPanel>
<TextBlock Grid.Column="1" Text="{Binding Path=Items, Converter={StaticResource SuTotals}, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,15,0" TextAlignment="Center"/>
</Grid>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
</Grid>
</Window>
这是背后的代码:
VB.NET
Public Class SuTotals
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Dim rows As IEnumerable(Of Object) = CType(value, IEnumerable(Of Object))
Dim c As Integer = 0
For Each item As Object In rows
c += item("Amount")
Next
Return c
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException
End Function
End Class
C#
public class SuTotals : IValueConverter
{
public object IValueConverter.Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture)
{
IEnumerable<object> rows = (IEnumerable<object>)value;
int c = 0;
foreach (object item in rows) {
c += item("Amount");
}
return c;
}
public object IValueConverter.ConvertBack(object value, Type targetType, object parameter, Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
该数据集被命名为 DataSet1 ,该表被命名为销售。它有2列,分别为描述作为字符串和 Amount 作为Double。