2012-05-30 51 views
3

我想通過下面的簡單公式MVVM計算出「NetAmount」計算結果來改變的MVVM

GrossAmount +回車 - 折扣= NetAmount

我使用MVVM光工具包和聲明的屬性如下

public const string DiscountPropertyName = "Discount"; 
private double _discount; 
public double Discount 
{ 
    get 
    { 
     return _discount; 
    } 

    set 
    { 
     if (_discount == value) 
     { 
      return; 
     } 
     _discount = value; 
     // Update bindings, no broadcast 
     RaisePropertyChanged(DiscountPropertyName); 
    } 
} 

public const string CarriagePropertyName = "Carriage"; 
private double _carriage; 

public double Carriage 
{ 
    get 
    { 
     return _carriage; 
    } 
    set 
    { 
     if (_carriage == value) 
     { 
      return; 
     } 
     _carriage = value; 
     RaisePropertyChanged(CarriagePropertyName); 
    } 
} 

public const string NetAmountPropertyName = "NetAmount"; 
private double _netAmount; 

public double NetAmount 
{ 
    get 
    { 
     _netAmount = Carriage + Discount; 
     return _netAmount; 
    } 
    set 
    { 
     if (_netAmount == value) 
     { 
      return; 
     } 
     _netAmount = value; 
     RaisePropertyChanged(NetAmountPropertyName); 
    } 
} 

public const string GrossAmountPropertyName = "GrossAmount"; 
private double _grossAmount; 

public double GrossAmount 
{ 
    get 
    { 
     return _grossAmount; 
    } 

    set 
    { 
     if (_grossAmount == value) 
     { 
      return; 
     } 
     _grossAmount = value; 
     RaisePropertyChanged(GrossAmountPropertyName); 
    } 
} 

我在XAML綁定這些屬性的文本框喜歡如下:

<TextBox Text="{Binding GrossAmount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding Mode=OneWay}"/> 

<TextBox Text="{Binding Carriage, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding Mode=OneWay}"/> 

<TextBox Text="{Binding Discount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding Mode=OneWay}"/> 

我綁定的NetAmount屬性文本塊一樣如下:

<TextBlock Text="{Binding NetAmount}" /> 

視圖模型是SalesOrderViewModel

我不知道我在哪裏放上述公式,以便它在任何文本框的值發生變化時導致更改NetAmount屬性。

我對C#並不陌生,但是對MVVM來說是新手,並且我知道有一些非常小的愚蠢的事情我做錯了,但是我無法理解它。

任何幫助將不勝感激。

回答

4

由於NetAmount是一種計算方法,因此可以將其視爲您的視圖模型中的只讀屬性。訪問屬性實際上執行計算。最後一個訣竅是調用RaisePropertyChanged(NetAmountProperty)每當任何的影響NetAmount變化的因素

public const string GrossAmountPropertyName = "GrossAmount"; 
private double _grossAmount; 
public double GrossAmount 
{ 
    get { return _grossAmount; } 
    set 
    { 
      if (_grossAmount == value) 
       return; 

      RaisePropertyChanged(GrossAmountPropertyName); 
      RaisePropertyChanged(NetAmountPropertyName); 
    } 
} 

public double Discount{} ... //Implement same as above 
public double Carriage {} ... //Implement same as above 

public const string NetAmountPropertyName = "NetAmount"; 
public double NetAmount 
{ 
    get { return GrossAmount + Carriage - Discount; } 
} 

編輯:如果你不想調用RaisePropertyChanged添加到每一個影響NetAmount那麼你可以財物修改RaisePropertyChanged,這樣它也會爲NetAmount屬性引發PropertyChanged事件。這會造成一些不必要的PropertyChanged事件被提出,但會更容易維護

private void RaisePropertyChanged(string propertyName) 
{ 
    var handler = PropertyChanged; 
    if (handler != null) 
    { 
      handler(this, new PropertyChangedEventArgs(propertyName); 
      handler(this, new PropertyChangedEventArgs(NetAmountProperty); 
    } 
} 
+0

很好,謝謝,不僅解決了我的問題,但也幫助我理解這個概念 – Thr3e

+0

我有同樣的問題,並審議了該解決方案,但拒絕了它。問題在於公共財產GrossAmount需要知道所有使用它的東西,這在我看來是非常糟糕的做法。如果有任何新客戶使用GrossAmount,則需要更改GrossAmount的代碼。 :( – digitig

+0

@digitig請參閱上面的編輯 – cordialgerm