如果数字大于某个值,如何更改文本框中的字体颜色

问题描述 投票:0回答:2

如果数字大于5,我想将文本框中的文本颜色更改为蓝色。

最简单的方法是什么?

我以为会有一个简单的textbox.FontColor = Blue;,但找不到类似的东西

c# wpf xaml datatrigger
2个回答
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();
    }
}

2
投票
textbox.Foreground = new SolidColorBrush(Colors.Blue);
© www.soinside.com 2019 - 2024. All rights reserved.