I need a help to to make a summary row or total a row in WPF datagrid using MVVM pattern, special of this summary row is having a value for each column like a image shows below.First total calculation base on first 3 items and those are in one group.I couldn't find a good example or sample code for this issue.
Please refer this image:
Several solutions are possible. There is not enough detail in your explanations to select the most appropriate option.
Below is an example using the converter for summarizing by group and styling the group in the 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.