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

时间:2015-03-03 05:14:45

标签: c# wpf mvvm row summary

我需要一个帮助来使用MVVM模式在WPF数据网格中创建一个摘要行或总计一行,此摘要行的特殊值是每列的值,如下图所示。第一个总计算基于前3个项目这些是在一个组中。我找不到这个问题的好例子或示例代码。

请参考此图片:

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以在数据网格中添加页脚行,例如This或类似This

- 更新 -

如果你需要行分组看看Here ..尝试理解分组和添加行组头的概念

答案 1 :(得分:0)

有几种可能的解决方案。您的解释不够详细,无法选择最合适的选项。

以下是使用转换器按组汇总并在 DataGrid 中设置组样式的示例。

<?php
$path13 = "Jobtype.php?skill=$s1&jobtype=$value5&Allloca=$l1";
?>
<select>
  <a href="<?= $path13 ?>"><option value="<?= $path13 ?>" selected><?= $value5 ?></option>
</select>
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;
    }

}