繁体   English   中英

如何通过 MVVM 在 WPF 数据网格中实现汇总行(总行)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM