如果超过某个值,如何更改文本框中的字体

时间:2016-09-13 15:54:18

标签: c# wpf xaml datatrigger

如果超过5,我想将文本框中文本的颜色更改为蓝色。

最简单的方法是什么?

我以为会有一个简单的textbox.FontColor = Blue;但是我找不到这样的东西

2 个答案:

答案 0 :(得分:7)

您需要数据触发器和值转换器 -

<DataTrigger
                Binding="{Binding Path=PROPERTY,
                Converter={StaticResource GreaterThanConverter},
                ConverterParameter=5}"
                Value="True">
                <Setter Property="TextBox.Foreground" Value="Blue" />

转换器可能看起来像 -

 public class GreaterThanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var limit = (int)parameter;
        return (int)value > limit;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

答案 1 :(得分:2)

textbox.Foreground = new SolidColorBrush(Colors.Blue);
相关问题