[英]How to implement summary rows(Total rows) in WPF data-grid via MVVM
我需要幫助來使用 MVVM 模式在 WPF 數據網格中創建摘要行或總計一行,此摘要行的特殊之處在於每一列都有一個值,如下圖所示。第一個總計算基於前 3 個項目,這些是在一組中。我找不到這個問題的好例子或示例代碼。
請參考這張圖片:
幾種解決方案是可能的。 您的解釋中沒有足夠的細節來選擇最合適的選項。
下面是一個使用轉換器按組匯總並在 DataGrid 中設置組樣式的示例。
namespace TotalRows
{
public class ItemClass
{
public int Group { get; set; }
public string Title { get; set; }
public int Y2013 { get; set; }
public int Y2014 { get; set; }
public int Y2015 { get; set; }
public int Y2016 { get; set; }
}
}
using System.Collections.ObjectModel;
using System.Linq;
namespace TotalRows
{
public class ExampleData
{
public static ObservableCollection<ItemClass> Items { get; }
= new ObservableCollection<ItemClass>()
{
new ItemClass() {Group=1, Title="Item1", Y2013=1200, Y2014=1500, Y2015=1800, Y2016=1500},
new ItemClass() {Group=1, Title="Item2", Y2013=2350, Y2014=2000, Y2015=2400, Y2016=2300},
new ItemClass() {Group=1, Title="Item3", Y2013=4000, Y2014=4350, Y2015=5000, Y2016=5500},
new ItemClass() {Group=2, Title="Item1", Y2013=1250, Y2014=1400, Y2015=1900, Y2016=1500},
new ItemClass() {Group=2, Title="Item2", Y2013=1350, Y2014=2500, Y2015=2450, Y2016=2700},
new ItemClass() {Group=2, Title="Item3", Y2013=3500, Y2014=3350, Y2015=5000, Y2016=5500},
};
}
}
using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace TotalRows
{
public class TotalItemsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is CollectionViewGroup group)
{
switch (parameter)
{
case "13": return group.Items.OfType<ItemClass>().Sum(item => item.Y2013);
case "14": return group.Items.OfType<ItemClass>().Sum(item => item.Y2014);
case "15": return group.Items.OfType<ItemClass>().Sum(item => item.Y2015);
case "16": return group.Items.OfType<ItemClass>().Sum(item => item.Y2016);
}
}
return DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public static TotalItemsConverter Instance { get; } = new TotalItemsConverter();
}
public class TotalItemsConverterExtension : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
=> TotalItemsConverter.Instance;
}
}
<Window x:Class="TotalRows.TotalRowsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TotalRows"
mc:Ignorable="d"
Title="RowsTotalWindow" Height="450" Width="800">
<FrameworkElement.Resources>
<CollectionViewSource x:Key="items" Source="{x:Static local:ExampleData.Items}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Group"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</FrameworkElement.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Mode=OneWay, Source={StaticResource items}}">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Converter={local:TotalItemsConverter}, ConverterParameter=13}" Margin="95,0,0,0"/>
<TextBlock Text="{Binding Converter={local:TotalItemsConverter}, ConverterParameter=14}" Margin="15,0,0,0"/>
<TextBlock Text="{Binding Converter={local:TotalItemsConverter}, ConverterParameter=15}" Margin="15,0,0,0"/>
<TextBlock Text="{Binding Converter={local:TotalItemsConverter}, ConverterParameter=16}" Margin="15,0,0,0"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
</Grid>
</Window>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.