此主题描述了如何在gridcontrol中的视图模型和显示定义总计和分组统计
在视图模型中指定统计
1、创建 SummaryItemType 枚举你想要在GridControl中显示的统计类型:
public enum SummaryItemType { Max, Count, None }
2、创建一个grid统计描述类
public class Summary : BindableBase {
public Summary(SummaryItemType type, string fieldname) {
Type = type;
FieldName = fieldname;
}
public SummaryItemType Type { get; }
public string FieldName { get; }
}
3、在视图模型中指定合计和分组统计集合
public class CustomersViewModel : ViewModelBase {
public CustomersViewModel() {
// ...
TotalSummary = new ObservableCollection<Summary>() {
new Summary(SummaryItemType.Count, nameof(Customer.Name)),
new Summary(SummaryItemType.Max, nameof(Customer.Visits))
};
GroupSummary = new ObservableCollection<Summary>() {
new Summary(SummaryItemType.Count, nameof(Customer.Name))
};
}
// ...
public ObservableCollection<Summary> TotalSummary { get; }
public ObservableCollection<Summary> GroupSummary { get; }
}
如果你需要GridControl引用在TotalSummary和GroupSummary集合中的改变,这些集合必须实现INotifyCollectionChanged 接口
创建统计模板
1、指定一个统计类型(Min,Max,Count),设置SummaryItemBase.SummaryType属性到SummaryItemType枚举值。创建ObjectToObjectConverter来映射SummaryItemType枚举值到SummaryItemType枚举值:
<Window ...
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
<Window.Resources>
<dxmvvm:ObjectToObjectConverter x:Key="SummaryTypeConverter">
<dxmvvm:MapItem Source="Max" Target="Max" />
<dxmvvm:MapItem Source="Count" Target="Count" />
<dxmvvm:MapItem Source="None" Target="None" />
</dxmvvm:ObjectToObjectConverter>
</Window.Resources>
<!-- ... -->
</Window>
2、创建一个模板生成统计
使用SummaryTypeConverter去指定Converter参数对于SummaryItemBase.SummaryType属性
使用DependencyObjectExtensions.DataContext附加属性当你绑定到GridControl统计属性。这个附加的属性同步数据更新束缚属性提高grid性能:
<Window.Resources>
<DataTemplate x:Key="SummaryTemplate">
<ContentControl>
<dxg:GridSummaryItem FieldName="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}"
SummaryType="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Type, RelativeSource={RelativeSource Self}, Converter={StaticResource SummaryTypeConverter}}"/>
</ContentControl>
</DataTemplate>
</Window.Resources>
在GridControl中显示视图模型统计
1、绑定TotalSummarySource和GroupSummarySource属性到总计和分组统计集合
2、设置TotalSummaryGeneratorTemplate和GroupSummaryGeneratorTemplate属性到模板生成统计
3、设置DataViewBase.TotalSummaryPosition属性到Top和Bottom显示Total Summary Panel
<dxg:GridControl ...
TotalSummarySource="{Binding TotalSummary}"
GroupSummarySource="{Binding GroupSummary}"
TotalSummaryGeneratorTemplate="{StaticResource SummaryTemplate}"
GroupSummaryGeneratorTemplate="{StaticResource SummaryTemplate}">
<dxg:GridControl.View>
<dxg:TableView TotalSummaryPosition="Bottom"/>
</dxg:GridControl.View>
</dxg:GridControl>