Change foreground color of textbox when text changes and meets certain criterion

10,707

Solution 1

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.

Here is the code in 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;
    }
}

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".

Solution 2

You should just be able to plug into the TextChanged event in wpf and bind a method to this event in the XAML. Then you could check to see if the new values meets your criteria and change the color accordingly.

I'm not really sure what you mean by the "XAML approach" but in this case when you simply want to attach behavior to an event thats raised on one of your controls I do not think that it is wrong to do it the way you have already tried using TextChanged. That's why events are visible in XAML in the first place.

Share:
10,707
Aarohi S
Author by

Aarohi S

Updated on June 19, 2022

Comments

  • Aarohi S
    Aarohi S almost 2 years

    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.

    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.

  • Aarohi S
    Aarohi S almost 11 years
    Thanks. I have this textbox: <TextBox Name="txt_Test" TextChanged="TextBox_TextChanged" /> and I have coded as: private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { if (txt_Test.Text == IsANumber) txt_Test.Foreground = Brushes.Red; else txt_Test.Foreground = Brushes.Green; } But, I need different approach using trigger/datatrigger from xaml or any other way possible.
  • Jesse Carter
    Jesse Carter almost 11 years
    I'm confused as to why you "need" a different approach here? I don't see anything wrong with the way you're doing things right now. Is it a requirement from your employer or something?
  • Aarohi S
    Aarohi S almost 11 years
    Yes, it is a requirement to use xaml approach rather than behind code appraoch.
  • Aarohi S
    Aarohi S almost 11 years
    I implemented this and it works perfectly. Thank you very much for the help :)
  • Colin
    Colin almost 11 years
    @AarohiS It is my pleasure. Could you pls mark my post as the answer for your quesion? Thank you very much. :)