我想让 WPF 文本框的 Foreground 属性变为红色,只要它的 Text 属性与表单上另一个文本框的 Text 属性不匹配。
我可以在后面的代码中并通过与转换器的绑定(bind)来完成此操作。但是有没有办法只在 XAML 中做到这一点? (我在想某种触发器)。
最佳答案
不,你需要代码。该代码可能在转换器中:
<TextBox x:Name="_textBox1"/>
<TextBox Foreground="{Binding Text, ElementName=_textBox1, Converter={StaticResource ForegroundConverter}}"/>
或者在 View 模型中:
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; }
}
关于.net - WPF:如何根据 XAML 中另一个的 Text 属性更改文本框的前景色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1353730/