2012-05-20 85 views
0

我正在創建一個貸款計算器,我有3個文本框,我試圖去反映出另一個。在我更改其中一個文本框的輸入後,我想讓其他2根據輸入重新計算,方法是按下我的程序中的計算按鈕,或者在文本框失去焦點後按下。屬性值反映了他人的

的三個文本框我有被綁定到會在這個程序被運行以及在運行時可以改變的能力,設置一定的值:

 <TextBox Width="55" Height="23" Style="{StaticResource ResourceKey=StandardTextbox}" Text="{Binding StringFormat={}{0:P}, Path=CurrentLoan.PropertyTaxRate, Converter={StaticResource TextBoxToDecimalConverter1}}"/> 

    <TextBox Width="65" Height="23" Style="{StaticResource ResourceKey=StandardTextbox}" Text="{Binding StringFormat={}{0:C}, Path=CurrentLoan.PropertyTaxMonth, Converter={StaticResource TextBoxToDecimalConverter1}}"/> 

    <TextBox Width="65" Height="23" Style="{StaticResource ResourceKey=StandardTextbox}" Text="{Binding StringFormat={}{0:C}, Path=CurrentLoan.PropertyTaxYear, Converter={StaticResource TextBoxToDecimalConverter1}}"/> 

    CurrentLoan.PropertyTaxRate = .012; 
    CurrentLoan.PropertyTaxMonth = 250; 
    CurrentLoan.PropertyTaxYear = 3000; 

    SharedValues.HomeValue = 250000; 

對於具有價值的家250,000美元,稅率爲1.2%,每年支付3000美元(250,000 * .012),每月支付250美元(一年3,000/12個月)。

而且我有已經聲明,像這樣的屬性:

public double PropertyTaxRate 
    { 
    get { return _propertyTaxRate; } 
    set { SetValueAndNotify(() => PropertyTaxRate, ref _propertyTaxRate, (value > 1) ? value/100 : value); } 
    } 

    public double PropertyTaxMonth 
    { 
    get { return _propertyTaxMonth; } 
    set { SetValueAndNotify(() => PropertyTaxMonth, ref _propertyTaxMonth, value); } 
    } 

    public double PropertyTaxYear 
    { 
    get{ return _propertyTaxYear; } 
    set { SetValueAndNotify(() => PropertyTaxYear, ref _propertyTaxYear, value); } 
    } 

的SetValueAndNotify方法只是簡單地將值設置爲後盾財產,並通知物業已經使用INotifyPropertyChanged的改變的GUI。

說如果我將PropertyTaxYear屬性更改爲$ 6000,我希望PropertyTaxMonth更改爲$ 500,PropertyTaxRate更改爲2.4%。任何人有關於如何做到這一點的想法,並使這些屬性相互反映?先進的謝謝您的意見!

+0

什麼問題? – Rahul

+0

對不起。在最後一段中更清楚。 –

+0

SetAndNotify使用INotifyPropertyChanged? – Berryl

回答

1

試試這個,並小心避免無休止的遞歸。它有助於檢查if (value != <old value>)。在設置其他相關屬性之前,請先撥打SetValueAndNotify,以便此檢查生效。

public double PropertyTaxRate 
{ 
    get { return _propertyTaxRate; } 
    set { 
     if (value > 1) { 
      value /= 100; 
     } 
     if (value != _propertyTaxRate) { 
      SetValueAndNotify(() => PropertyTaxRate, ref _propertyTaxRate, value); 
      PropertyTaxYear = value * SharedValues.HomeValue; 
     } 
    } 
} 

public double PropertyTaxMonth 
{ 
    get { return _propertyTaxMonth; } 
    set { 
     if (value != _propertyTaxMonth) { 
      SetValueAndNotify(() => PropertyTaxMonth, ref _propertyTaxMonth, value); 
      PropertyTaxYear = 12 * value; 
     } 
    } 
} 

public double PropertyTaxYear 
{ 
    get{ return _propertyTaxYear; } 
    set { 
     if (value != _propertyTaxYear) { 
      SetValueAndNotify(() => PropertyTaxYear, ref _propertyTaxYear, value); 
      PropertyTaxMonth = value/12; 
      PropertyTaxRate = value/SharedValues.HomeValue; 
     } 
    } 
} 

UPDATE

遞歸問題就更難於預期。由於SetValueAndNotify可能會觸發意外行爲,因此我建議對所有支持變量進行計算並在完成時通知。 (我沒有顯示仍然創建OnNotifyPropertyChanged方法的代碼。)

private void Notify() 
{ 
    OnNotifyPropertyChanged(() => PropertyTaxRate); 
    OnNotifyPropertyChanged(() => PropertyTaxYear); 
    OnNotifyPropertyChanged(() => PropertyTaxMonth); 
} 

public double PropertyTaxRate 
{ 
    get { return _propertyTaxRate; } 
    set { 
     if (value > 1) { 
      value /= 100; 
     } 
     if (value != _propertyTaxRate) { 
      _propertyTaxRate = value; 
      _propertyTaxYear = value * SharedValues.HomeValue; 
      _propertyTaxMonth = _propertyTaxYear/12; 
      Notify(); 
     } 
    } 
} 

public double PropertyTaxMonth 
{ 
    get { return _propertyTaxMonth; } 
    set { 
     if (value != _propertyTaxMonth) { 
      _propertyTaxMonth = value; 
      _propertyTaxYear = 12 * value; 
      _propertyTaxRate = _propertyTaxYear/SharedValues.HomeValue; 
      Notify(); 
     } 
    } 
} 

public double PropertyTaxYear 
{ 
    get{ return _propertyTaxYear; } 
    set { 
     if (value != _propertyTaxYear) { 
      _propertyTaxYear = value; 
      _propertyTaxMonth = value/12; 
      _propertyTaxRate = value/SharedValues.HomeValue; 
      Notify(); 
     } 
    } 
} 
+0

嗯,它在我身上拋出一個例外,那裏有if語句。無法發生StackOverflow。我還用下劃線更改了支持屬性 –

+0

下劃線(_)缺失。 (SO回答編輯器在雙擊時不能選擇整個標識符。) –

+0

仍然不行。由於某種原因得到例外 –