2016-05-17 5 views
-1

ObservableCollection<Item>にバインドされたDataGridがあります。アイテムの数量を変更すると、アイテムの合計が自動的に変更されます。C#mvvmを使用して何かが変更されるたびに他の値を変更する

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; 
    } 
} 

「ViewModelには、単純異なっている請求書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> 

れているように見える:

私は何だった
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 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 ViewModel(){ 
    Invoice.Items.Quantity.PropertyChanged += (s,e) 
    { 
     Total = Cost*Quantity 
    } 
} 



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

ただし、コンパイルされません。

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

なぜ、あなたは、現実化されているアイテムのセッターのRefreshofをしないのですか? private int _quantity; public int量 { get {return _quantity; } セット { if(_quantity!= value) { _quantity = value; RefreshSomething(); OnPropertyChanged(); } } } – SeeuD1

+0

これを試してください: 'public decimal Total {get {return Cost * Quantity; }} '(そして' OneWay'バインディング)。 – Jose

答えて

1

それならばあなただけの、計算されたプロパティとして合計を実装することができ、あなたのQuantityプロパティにOnPropertyChanged("Total");を追加

1

てみてください冗長データを格納する点がないため、常にCost * Quantityと同じです。

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

パラメータがないOnPropertyChanged()が、登録されたすべてのプロパティを再チェックする汎用/ nullプロパティ変更イベントを発生させると仮定すると、これはうまくいくはずです。

+0

ここでは「合計」を設定してはいけませんが、Ben Jacksonの答えと同じように完全に計算してください。 –

0

あなたがそうのようなあなたの財産の中に書かれたコードを持つことができます::

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(); 
     } 
    } 
} 
関連する問題