2015-12-28 104 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