假设我有销售价格,首付款金额,首付款百分比和贷款金额。当用户更改这些属性中的任何一个时,需要更新其他属性以反映新值。你如何处理这种类型的无限属性变化事件?
当需要跨多个属性进行流量控制时,我将建立一个流量控制变量-一个布尔值-并且在每个正在更改的属性中,我将添加一个测试来查看我是否处于流量控制之下。
private bool controlledChange = false;
public property int MyVal1
{
set
{
_myVal1 = value;
if(!controlledChange)
{
controlledChange = true;
MyVal2 -= 1;
controlledChange = false;
}
}
}
public property int MyVal2
{
set
{
_myVal2 = value;
if(!controlledChange)
{
controlledChange = true;
MyVal1 += 1;
controlledChange = false;
}
}
}
这样,无论属性发生了什么变化,都可以启动其他属性的变化,但是当它们发生变化时,它们将不会依次启动它们自己的一组变化。
如果这些属性可以产生计算结果,那么您还应该尽可能多地将这些属性设置为只读,以便限制对象的更改方式。
最简单的方法是仅在属性确实发生更改时引发更改事件:
public decimal SalePrice {
get{
return salePrice;
}
set {
if (salePrice != value) {
salePrice = value; // putting as first statement prevents the setter
// to be entered again ...
RaiseSalePriceChange();
// Set other properties
}
}
}
我不确定我完全理解,因为我不知道你说的"无限"是什么意思
这可能是一个用字段支持属性的好用例。这样,您就可以在属性集上触发事件,但在内部一次只设置一个字段,而不会触发N个事件。
class MyClass
{
private string m_Name;
private int m_SomeValue;
public string Name
{
get { return m_Name; }
set
{
if (value != m_Name)
{
m_Name = value;
m_SomeValue++;
// Raise Event
}
}
}
public int SomeValue
{
get { return m_SomeValue; }
set
{
if (m_SomeValue != value)
{
m_SomeValue = value;
// Raise Event
}
}
}
如果INotifyPropertyChanged
确实需要通知外部对象,那么我将只集中所有内容。这样的:
private double salesPrice;
private double downPaymentAmount;
private double downPaymentPercent;
private double loanAmount;
public double SalesPrice
{
get
{
return salesPrice;
}
set
{
if (salesPrice != value)
{
salesPrice = value;
// maybe you would rather use a RecalculateForSalePriceChanged() method
RecalculateDownPaymentAmount();
RecalculateDownPaymentPercent();
RecalculateLoanAmount();
propertiesChanged();
}
}
}
public double DownPaymentAmount
{
get
{
return downPaymentAmount;
}
set
{
if (downPaymentAmount != value)
{
downPaymentAmount = value;
// see above
RecalculateDownPaymentPercent();
RecalculateLoanAmount();
RecalculateSalesPrice();
propertiesChanged();
}
}
}
public double DownPaymentPercent
{
get
{
return downPaymentPercent;
}
set
{
if (downPaymentPercent != value)
{
downPaymentPercent = value;
// see above
RecalculateDownPaymentAmount();
RecalculateLoanAmount();
RecalculateSalesPrice();
propertiesChanged();
}
}
}
public double LoanAmount
{
get
{
return loanAmount;
}
set
{
if (loanAmount != value)
{
loanAmount = value;
// see above
RecalculateDownPaymentAmount();
RecalculateDownPaymentPercent();
RecalculateSalesPrice();
propertiesChanged();
}
}
}
private void propertiesChanged()
{
RaisePropertyChanged("SalesPrice", "DownPaymentAmount", "DownPaymentPercent", "LoanAmount");
}
也许你可以集中在更少的方法,甚至一个单一的重新计算,但我不知道你是如何计算他们。但是,在重新计算这些值时,必须保持特定的顺序。因为它们只操作字段而不改变属性,所以不会有propertychanged -feedback循环。
希望这对你有帮助,我没有误解你想要的。
OP想要的是如下内容
class A : INotifyPropertyChanged
{
private int field1;
public int Property1
{
get { return field1; }
set
{
field1 = value;
field2++;
RaisePropertyChanged("Property1");
RaisePropertyChanged("Property2");
}
}
private int field2;
public int Property2
{
get { return field2; }
set
{
field2 = value;
field1++;
RaisePropertyChanged("Property1");
RaisePropertyChanged("Property2");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
他可能正在处理他提到的每个属性的setter中的其他属性,从而导致循环调用setter。
Vijay