tags:

views:

305

answers:

1

I want to make the Foreground property of a WPF Textbox red as long as its Text property does not match the Text property of another Textbox on the form. I can accomplish this in the code behind and through a binding with a converter. But is there a way to do it in XAML only? (I was thinking of a Trigger of some kind).

+2  A: 

No, you need code. That code could be in a converter:

<TextBox x:Name="_textBox1"/>
<TextBox Foreground="{Binding Text, ElementName=_textBox1, Converter={StaticResource ForegroundConverter}}"/>

Or in a view model:

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; }
}

HTH, Kent

Kent Boogaart
Thanks, the viewmodel code is useful, I have never met the pattern before. I will go for the converter then, though.
Dabblernl