2009-08-30 7 views
2

フォーム上の別のテキストボックスのTextプロパティとTextプロパティが一致しない限り、WPF TextboxのForegroundプロパティを赤にしたいとします。 私はこのコードをコンバーターとコンバーターを使ってバインドすることでこれを実現できます。しかし、XAMLだけでそれを行う方法はありますか? (私はある種のトリガーを考えていた)。WPF:XAMLの別のTextプロパティに応じてテキストボックスの前景色を変更する方法はありますか

答えて

4

いいえ、コードが必要です。このコードは、コンバータに次のようになります。

<TextBox x:Name="_textBox1"/> 
<TextBox Foreground="{Binding Text, ElementName=_textBox1, Converter={StaticResource ForegroundConverter}}"/> 

またはビューモデルで:

public string FirstText 
{ 
    //get/set omitted 
} 

public string SecondText 
{ 
    get { return _secondText; } 
    set 
    { 
     if (_secondText != value) 
     { 
      _secondText = value; 
      OnPropertyChanged("SecondText"); 
      OnPropertyChanged("SecondTextForeground"); 
     } 
    } 
} 

public Brush SecondTextForeground 
{ 
    get { return FirstText == SecondText ? Brushes.Red : Brushes.Black; } 
} 
+0

おかげで、viewmodelのコードは便利ですが、私は前のパターンに会ったことがありません。私はその後、コンバータのために行くだろう。 – Dabblernl

関連する問題