2015-12-28 106 views
0

Bonjour!計算屬性的型號

我有誰包含許多經典特性(代碼,描述,價(含稅),價格不含增值稅,增值稅)產品型號。對於我的例子,我只提出有趣的屬性。

在這個模型中,我想補充的價格計算。當任何量的變化,計算其他屬性(如果還原的變化,重新計算價(含稅)和價格不含增值稅,反之亦然)

我的例子:

public class Product : EditableObject 
{ 

    //VAT percentage propertie (ex: 20%) 
    private decimal _vat; 
    public decimal Vat 
    { 
     get { return _vat; } 
     set 
     { 
      _vat = value; 
      PriceWithVat = _priceWithoutVat * (1 + Vat/100); //Vat changed, we recalculate Price WITH VAT propertie 
      PriceWithoutVat = _priceWithVat/(1 + Vat/100); //Vat changed, we recalculate Price WITHOUT VAT propertie 
      NotifyOfPropertyChange(() => Vat);     
     } 
    } 

    //Product Price Without VAT 
    private decimal _priceWithoutVat; 
    public decimal PriceWithoutVat 
    { 
     get { return _priceWithoutVat; } 
     set 
     { 
      _priceWithoutVat = value;     
      PriceWithVat = _priceWithoutVat * (1 + Vat/100); //PriceWithoutVat changed, we recalculate Price WITH VAT propertie 
      NotifyOfPropertyChange(() => PriceWithoutVat); 
     } 
    } 

    //Product Price WITH Vat 
    private decimal _priceWithVat; 
    public decimal PriceWithVat 
    { 
     get { return _priceWithVat; } 
     set 
     { 
      _priceWithVat = value;     
      PriceWithoutVat = _priceWithVat/(1 + Vat/100); //PriceWithVat changed, we recalculate Price WITHOUT VAT propertie 
      NotifyOfPropertyChange(() => PriceWithVat); 
     } 
    }  

} 

由此,在任何價格變化,我有無限循環和堆棧溢出。正常的,因爲當任何價格的變動,所有其他被重新計算,並重新計算它們又將價格:)

你有解決方案,自動重新計算我的3種金額時,它們中的任何改變?

回答

1

不要讓計算性能讀寫。相反,適當提高爲PropertyChanged只讀屬性計算,例如: -

public decimal Price 
{ 
    get { return _price; } 
    set 
    { 
     if (_price != value) 
     { 
      _price = value; 
      NotifyOfPropertyChange(() => Price); 
      NotifyOfPropertyChange(() => PriceWithVat); 
     } 
    } 
} 

public decimal Vat 
{ 
    get { return _vat; } 
    set 
    { 
     if (_vat != value) 
     { 
      _vat = value; 
      NotifyOfPropertyChange(() => Vat); 
      NotifyOfPropertyChange(() => PriceWithVat); 
     } 
    } 
} 

public decimal PriceWithVat 
{ 
    get { return _price/(1 + _vat/100); } 
} 

由於他們是計算屬性,你不能直接設置它們的值。另一方面,爲它們提高PropertyChanged足以從UI重新讀取它們的值。

UPDATE

根據您的評論(儘管這是一個非常奇怪要求),您可以通過更新支持字段值達到你想要什麼。請注意,你不能調用屬性setter這裏以避免StackoverflowException

private void UpdatePriceWithVat() 
{ 
    _priceWithVat = _price * (1 + _vat/100); 
    NotifyOfPropertyChange(() => PriceWithVat); 
} 

private void UpdatePrice() 
{ 
    _price = _priceWithVat/(1 + _vat/100); 
    NotifyOfPropertyChange(() => Price); 
} 

public decimal Price 
{ 
    get { return _price; } 
    set 
    { 
     if (_price != value) 
     { 
      _price = value; 
      NotifyOfPropertyChange(() => Price); 
      UpdatePriceWithVat(); 
     } 
    } 
} 

public decimal Vat 
{ 
    get { return _vat; } 
    set 
    { 
     if (_vat != value) 
     { 
      _vat = value; 
      NotifyOfPropertyChange(() => Vat); 
      UpdatePriceWithVat(); 
     } 
    } 
} 

public decimal PriceWithVat 
{ 
    get { return _priceWithVat; } 
    set 
    { 
     if (_priceWithVat != value) 
     { 
      _priceWithVat = value; 
      NotifyOfPropertyChange(() => PriceWithVat); 
      UpdatePrice(); 
     } 
    } 
} 

有兩點需要注意:

  • 術語「計算」這裏不是最好的選擇;
  • 如果您將添加更多屬性,這將影響其他屬性,此類代碼的複雜性將增長得非常快。
+0

謝謝丹尼斯。但在你的例子中,如果我想分配PriceWithVat價格會發生什麼? WITHOUT沒有計算? – Damosse31

+0

我希望我的用戶在我的應用程序中免費提供有或沒有增值稅的價格。 – Damosse31

+0

它工作正常!謝謝丹尼斯 – Damosse31