0

私は Loan Calculator を作成しており、相互に反映させようとしている 3 つの TextBox があります。TextBoxes の 1 つの入力を変更した後、プログラムにある計算ボタンを押すか、TextBox がフォーカスを失った後に、入力に基づいて他の 2 つを再計算します。

私が持っている 3 つの TextBoxes は、実行中のプログラムで設定される特定の値と、実行時に変更できる機能にバインドされています。

     <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% の住宅の場合、年間の支払いは 3,000 ドル (250,000 * .012) で、毎月の支払いは 250 ドル (3,000 / 1 年の 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 プロパティを $6,000 に変更した場合、PropertyTaxMonth を $500 に変更し、PropertyTaxRate を 2.4% に変更するとします。これを行う方法とプロパティを相互に反映させる方法についてアイデアを持っている人はいますか? ご意見をお寄せいただきありがとうございます。

4

1 に答える 1

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

アップデート

再帰問題は予想以上に難しいです。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();
        }
    }
}
于 2012-05-20T20:34:58.663 に答える