更新总计导致无限循环
本文关键字:无限循环 计导 更新 | 更新日期: 2023-09-27 18:32:25
我目前有3个telerik:RadMaskedCurrencyInput。
第一个是校长
第一和第二是支出
目前,我已经设置了本金=第一次支付+第二次支付。
我正在尝试一个案例,如果我更新本金,它会更新第一笔付款并将第二笔付款设置为零。所以本金 = 支付一和支付二 = 0
// Pv is principal
private decimal?_pv;
public decimal? Pv { get { return _pv; } set { _pv = value; OnPropertyChanged("Pv"); } }
private decimal? _disbursalOne;
public decimal? DisbursalOne
{
get
{
return _disbursalOne;
}
set
{
_disbursalOne = value;
if (_disbursalOne != null)
DisbursalTotal = _disbursalOne + DisbursalTwo;
else
{
_disbursalOne = 0;
}
OnPropertyChanged("DisbursalOne");
}
}
支付二与支付一几乎相同,因此守则不是必需的。
private decimal? _disbursalTotal;
public decimal? DisbursalTotal
{
get { return _disbursalTotal; }
set
{
_disbursalTotal = value;
if (_disbursalTotal != null) UpdateDisbursalTotal(_disbursalTotal);
else UpdateDisbursalTotal(0);
Pv = _disbursalTotal;
OnPropertyChanged("DisbursalTotal");
}
}
对于糟糕的标题,我深表歉意。
// Updates the Total Disbursed Fields on the UI
public void UpdateDisbursalTotal(decimal? Total)
{
var cultureInfo = Thread.CurrentThread.CurrentCulture; // You can also hardcode the culture, e.g. var cultureInfo = new CultureInfo("fr-FR"), but then you lose culture-specific formatting such as decimal point (. or ,) or the position of the currency symbol (before or after)
var numberFormatInfo = (NumberFormatInfo)cultureInfo.NumberFormat.Clone();
numberFormatInfo.CurrencySymbol = "$"; // Replace with "$" or "£" or whatever you need
Double _total = (double) Total;
DisburseString = _total.ToString("C", numberFormatInfo);
}
private string _disburseString;
public string DisburseString { get { return _disburseString; } set { _disburseString = value; OnPropertyChanged("DisburseString"); } }
通常最好按以下方式格式化PropertyChanged
事件,这样,如果设置了相同的值,则不会通知任何人,因为实际上没有任何更改。 如果事情依赖于您的PropertyChanged
事件相互更新,这应该会打破循环:
private decimal? _disbursalTotal;
public decimal? DisbursalTotal
{
get { return _disbursalTotal; }
set
{
if (value != _disburseTotal) {
_disbursalTotal = value;
if (_disbursalTotal != null) UpdateDisbursalTotal(_disbursalTotal);
else UpdateDisbursalTotal(0);
Pv = _disbursalTotal;
OnPropertyChanged("DisbursalTotal");
}
}
}
逐步完成代码。我的猜测是OnPropertyChanged事件也会更改属性。您可以在调用 OnPropertyChanged 之前检查属性是否实际更改(或只是设置为相同的值)。