English 中文(简体)
WPF:如何根据XAML中另一个文本财产改变文本箱的原色?
原标题:WPF: How to change the Foreground color of a Textbox depending on the Text property of another in XAML?
  • 时间:2009-08-30 11:57:22
  •  标签:

I want to make the Foreground property of a WPF Textbox red as long as its Text property does not match the Text property of another Textbox on the form. I can accomplish this in the code behind and through a binding with a converter. But is there a way to do it in XAML only? (I was thinking of a Trigger of some kind).

最佳回答

页: 1 该法典可以改写:

<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; }
}
问题回答

暂无回答




相关问题
热门标签