Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: reverting brush to default/original

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;
    }
}
like image 227
Thomas Stock Avatar asked Aug 20 '09 14:08

Thomas Stock


3 Answers

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.

like image 104
Daniel Avatar answered Oct 19 '22 18:10

Daniel


You can grab the default colours from the class SystemColors

Here is the list of all system colours: http://msdn.microsoft.com/de-de/library/system.windows.systemcolors.aspx

Default background colour of the client area:

     _textbox.Background = SystemColors.WindowBrush;

Default text colour inside the client area:

     _textbox.SystemColors.WindowTextBrush
like image 15
Beauty Avatar answered Oct 19 '22 19:10

Beauty


I may be late to the party, but for future readers, you can also use Button.BackgroundProperty.DefaultMetadata.DefaultValue for this purpose. This is especially useful when you're using a Converter where you need to return a value and therefore cannot use ClearValue() call.

like image 4
dotNET Avatar answered Oct 19 '22 17:10

dotNET