更新Total导致无限循环

时间:2014-11-27 15:52:30

标签: c#

我目前有3个telerik:RadMaskedCurrencyInput。

第一个是校长

第一和第二是分配

目前我已经设置了这样的主体=第一次发放+第二次发放。

我正试图让一个案例,如果我更新校长,它会更新First Disbursal并将第二次分配设置为零。所以Principal = DisbursalOne和DisbursalTwo = 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");
    }
}

Disbursal Two与Disbursal几乎完全相同,因此不需要Code。

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"); } }

2 个答案:

答案 0 :(得分:3)

通过以下方式格式化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"); 
        }
    } 
}

答案 1 :(得分:0)

逐步完成代码。我的猜测是OnPropertyChanged事件也改变了一个属性。在调用OnPropertyChanged之前,您可以检查属性是否实际正在更改(或者只是设置为相同的值)。