【问题标题】:How to implement summary rows(Total rows) in WPF data-grid via MVVM如何通过 MVVM 在 WPF 数据网格中实现汇总行(总行)
【发布时间】:2015-03-03 05:14:44
【问题描述】:

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

请参考这张图片:

【问题讨论】:

  • 您在使用 wpf 基本数据网格吗?还是别的什么?
  • 直接把图片放在问题里
  • blogs.msdn.com/b/mrochon/archive/2009/10/16/… ,这是找到我的问题的唯一解决方案。任何人都知道如何在 WPF 桌面版中做到这一点?

标签: c# wpf mvvm row summary


【解决方案1】:

您可以像ThisThis 一样向数据网格添加页脚行

-- 更新了--

如果您需要行分组,请查看Here.. 尝试理解分组和添加行组标题的概念

【讨论】:

  • 感谢您的回复,这不是我需要的。 i.imgur.com/G1NjqqR.jpg - 根据图片,有两种类型的行,一种是其他行的父(总计)。但两行都在同一个网格视图中,我使用的是基本的 WPF 数据网格。
  • 我已经更新了ans,看看,如果你有任何问题,请告诉我。
  • 我之前尝试过这个链接。问题不在于对行进行分组,在我的网格中(i.imgur.com/G1NjqqR.jpg),you 可以看到组标题(总行)也具有与其他行相同类型的列值。
【解决方案2】:

有几种解决方案是可能的。您的解释中没有足够的细节来选择最合适的选项。

以下是使用转换器按组汇总并在 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>

【讨论】:

    猜你喜欢
    • 2021-12-18
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2013-12-08
    相关资源
    最近更新 更多