怎么添加汇总行啊……求高手

解决方案 »

  1.   

    看下这个例子:<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>
    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 { get; private 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 { get; set; }
    public DateTime Date { get; set; }
    public AccountType Type { get; set; }
    public string Account { get; set; }
    public string Memo { get; set; }

    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(this, new PropertyChangedEventArgs(propertyName));
    }
    }
    }