2012-05-20 93 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

仍然不行。由于某种原因得到例外 –