打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
wpf datagrid如何添加汇总行
看下这个例子:
XML/HTML code?
1
2
3
4
5
6
7
8
9
10
11
12
<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="" Height="400" Width="550" >
    <DockPanel>
        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
            <TextBlock>Total:</TextBlock>
            <TextBlock Margin="30, 0, 0, 0" Text="{Binding TotalDebit}" />
            <TextBlock Margin="30, 0, 30, 0" Text="{Binding TotalCredit}" />
        </StackPanel>
        <DataGrid ItemsSource="{Binding Items}" />
    </DockPanel>

C# code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var r = new Random();
            var testData = Enumerable.Range(1, 7).Select(i => new Voucher()
                {
                    No = 145+i,
                    Date = DateTime.Now.AddDays(r.Next(-5, 5)),
                    Type = (AccountType)r.Next(2),
                    Debit = r.Next(100),
                    Credit = r.Next(100),
                });
             
            this.DataContext = new Vouchers(testData);
        }
    }
    public class Vouchers : ObservableObject
    {
        public Vouchers(IEnumerable<Voucher> items)
        {
            Items = new ObservableCollection<Voucher>(items);
            Items.CollectionChanged += (s, e)=> CalcTotals();
            Items.ToList().ForEach(item => item.PropertyChanged += (s, e) => CalcTotals());
            CalcTotals();
        }
        public ObservableCollection<Voucher> Items { getprivate set; }
        void CalcTotals()
        {
            TotalCredit = Items.Sum(x => x.Credit);
            TotalDebit = Items.Sum(x => x.Debit);
        }
        private decimal _totalDebit;
        public decimal TotalDebit
        {
            get return _totalDebit; }
            set { _totalDebit = value; OnPropertyChanged("TotalDebit"); }
        }
        private decimal _totalCredit;
        public decimal TotalCredit
        {
            get return _totalCredit; }
            set { _totalCredit = value; OnPropertyChanged("TotalCredit"); }
        }
    }  
  
    public enum AccountType { FinancialAccount, OtherAccount }
    public class Voucher : ObservableObject
    {
        public int No { getset; }
        public    DateTime Date { getset; }
        public    AccountType Type { getset; }
        public    string Account { getset; }
        public    string Memo { getset; }
         
        private decimal _debit;
        public    decimal Debit
        {
            get return _debit; }
            set { _debit = value; OnPropertyChanged("Debit"); }
        }
        private decimal _credit;
        public decimal Credit
        {
            get return _credit; }
            set { _credit = value; OnPropertyChanged("Credit"); }
        }
    }
    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
        }
    }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
EAI 导入时重新编码
WPF数据绑定-绑定到用户数据
wpf TreeView数据绑定
LilianChen C#:扩展方法
多态重构条件语句
快速定位元素
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服