WPF: reverting brush to default/original

Thomas Stock picture Thomas Stock · Aug 20, 2009 · Viewed 19.7k times · Source

I'm a complete newbie at WPF.

At the moment I'm making a usercontrol for form elements called "LabeledTextbox" which contains a label, a textbox and a textblock for errormessages.

When the using code adds an errormessage, I want to put the border of the textbox in red. But, when the errormessage gets removed, I'd like to turn back to the default bordercolor of the textbox. I feel there must be a very easy way to do this.

My code:

(in public partial class LabeledTextbox : UserControl)

public string ErrorMessage
{
    set
    {
        if (string.IsNullOrEmpty(value))
        {
            _textbox.BorderBrush = Brushes.Black; //How do I revert to the original color in the most elegant way?
        }
        else
        {
            _textbox.BorderBrush = Brushes.Red;
        }

        _errorMessage.Text = value;
    }
}

Answer

Daniel picture Daniel · Aug 20, 2009

You could use

_textBox.ClearValue(TextBox.BorderBrushProperty);

That will remove the directly assigned value and go back to the value defined by the style or template.