C#每次使用mvvm更改某些内容时都会更改其他值

C# Changing other values on everytime something is changed using mvvm

我有一个绑定到 ObservableCollection<Item>DataGrid。当我更改项目的数量时,我想自动更改项目的总数。这是 Quantity*Cost=Total

<DataGrid ItemsSource="{Binding Invoices.Items}" AutoGenerateColumns="False">
    <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
    <DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" />
    <DataGridTextColumn Header="Cost" Binding="{Binding Cost}" />
    <DataGridTextColumn Header="Total" Binding="{Binding Total}" />
</DataGrid>

ViewModel 非常简单

public class ViewModel:BaseClass
{
    public ViewModel()
    {
        FillInvoice();
    }

    private Invoice _invoice;
    public Invoice Invoice
    {
        get { return _invoice; }
        set
        {
            if (_invoice!=value)
            {
                _invoices = value;
                OnPropertyChanged();
            }
        }
    }

    private void FillInvoice()
    {
        var customer = new Customer() {Id=1,Name = "James"};
        var invoice = new Invoice() {Customer = customer, Id = 1,CustomerId = 1};
        var item = new Item() {Cost = Convert.ToDecimal(12.50),Id = 1,Name = "Item"};
        for (int i = 0; i < 10; i++)
        {
            invoice.Items.Add(item);
        }
        Invoices = invoice;
    }
}

发票看起来像:

public Invoices()
{
    Items=new ObservableCollection<Item>();
}
public int Id { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; }
public ObservableCollection<Item> Items { get; set; }

我的物品看起来像:

public class Item:BaseClass
{
    private string _name;
    private decimal _cost;
    public int Id { get; set; }
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name!=value)
            {
                _name = value;
                OnPropertyChanged();
            }
        }
    }

    private int _quantity;
    public int Quantity
    {
        get { return _quantity; }
        set
        {
            if (_quantity!=value)
            {
                _quantity = value;
                OnPropertyChanged();
            }
        }
    }

    public decimal Cost
    {
        get { return _cost; }
        set
        {
            if (_cost!=value)
            {
                _cost = value;
                OnPropertyChanged();
            }
        }
    }

    private decimal _total;

    public decimal Total
    {
        get { return _total; }
        set
        {
            if (_total != value)
            {
                _total = value;
                OnPropertyChanged();
            }

        }
    }

}

我当时想的是向数量项添加一个事件处理程序,它将为我计算总数,但我不确定该怎么做,我已经尝试添加了。

public ViewModel(){
    Invoice.Items.Quantity.PropertyChanged += (s,e)
    {
        Total = Cost*Quantity
    }
}



public Item()
{
    Quantity.PropertyChanged += (s,e)
    {
        Total = Cost*Quantity
    }
}

但它不能在它们上编译。

尝试将 OnPropertyChanged("Total"); 添加到您的 Quantity 属性:

private int _quantity;
public int Quantity
{
    get { return _quantity; }
    set
    {
        if (_quantity!=value)
        {
            _quantity = value;
            OnPropertyChanged();
            Total = Cost*Quantity
            OnPropertyChanged("Total");
        }
    }
}

您可以像这样在 属性 中编写代码:

private decimal _total;
private int _quantity;
private decimal _cost

public decimal Quantity
{
    get { return _quantity; }
    set 
    {
        _quantity = value;
        Total = _quantity * _cost;
    }
}

public decimal Total
{
    get { return _total; }
    set
    {
        if (_total != value)
        {
            _total = value;
            OnPropertyChanged();
        }
    }
}

如果 Total 始终等于 Cost*Quantity,您可以将 Total 实现为计算 属性,因为存储冗余数据没有意义:

public decimal Total
{
    get { return Cost * Quantity; }
}

假设没有参数的 OnPropertyChanged() 触发 generic/null 属性 更改事件,这将导致重新检查所有订阅的属性,这应该就可以正常工作了。