当文本更改并符合特定条件时,更改文本框的前景色 [英] Change foreground color of textbox when text changes and meets certain criterion

查看:69
本文介绍了当文本更改并符合特定条件时,更改文本框的前景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当文本在文本框内更改并符合特定条件时,我需要设置文本颜色.我可以通过 textbox_textchanged 事件并将brushs.color 设置为所需的颜色,从后面的代码中实现该功能.

I require to set text color when text changes inside textbox and meets certain criterion. I can implement this from code behind with textbox_textchanged event and set brushes.color to desired color.

但是我不能用xaml wpf方法实现这一点.我是wpf的新手,我不确定在文本框中更改文本时如何根据特定条件设置文本颜色.

But I am not being able to implement this with xaml wpf approach. I am new to wpf, I'm not sure how can I set text color depending upon certain criterion when text changes in textbox.

例如:对于给定的文本框,当文本更改时,需要确定输入的文本是否为数字,然后将前景色更改为绿色,否则为红色.

For example: For a given textbox, when text changes, it needs to determine if input text is a number then change foreground color to green else red.

期待获得帮助.预先谢谢你.

Looking forward for the help. Thank you in advance.

推荐答案

我不确定您所处的环境是否允许使用绑定转换器.但是,这里有一个解决方案,只需在您的代码后面加上一个绑定转换器即可.

I am not sure whether a binding converter is allowed in your situation. But here is a solution which only needs a binding converter in your code behind.

这是xaml中的代码

    <Grid.Resources>
        <local:ValueConverter x:Key="ValueConverter"></local:ValueConverter>
    </Grid.Resources>
    <TextBox Text="{Binding Text,UpdateSourceTrigger=PropertyChanged}">
        <TextBox.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Text,Converter={StaticResource ValueConverter}}" Value="True">
                        <Setter Property="TextBox.Foreground" Value="Red"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

这是视图模型和值转换器

Here is the view model and the value converter

public class ViewModel : INotifyPropertyChanged
{
    private string _text;

    public string Text
    {
        get
        {
            return this._text;
        }
        set
        {
            this._text = value;
            if (null != PropertyChanged)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Text"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

public class ValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (null != value)
        {
            if (value.ToString() == "1")
                return true;
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

因此,解决方案使用数据触发器来实现目标.在这里使用绑定转换器的唯一原因是您需要一个位置来确定应更改TextBox前景的值.当TextBox的值为"1"时,TextBox的前景将为红色.

So the solution uses the data trigger to fulfill the goal. The only reason for using binding converter here is that you need a place to determine what kind of value should change the foreground of the TextBox. Here the foreground of TextBox will be red when the value of the TextBox is "1".

这篇关于当文本更改并符合特定条件时,更改文本框的前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆