30

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;
    }
}
Thomas Stock
  • 10,927
  • 14
  • 62
  • 79

5 Answers5

51

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.

Daniel
  • 15,944
  • 2
  • 54
  • 60
  • 2
    great, thanks! *adds dependencyproperty research to the to-do list* – Thomas Stock Aug 20 '09 at 14:25
  • Thanks, very helpful article. I tried storing the default brush by the brush.clone method but according to .net no brush exists when its system default. Thank you! – JustinKaz May 27 '10 at 16:34
  • 2
    This didn't work for me. I set background to Aquamarine when I want to draw attention to it. I tried to use the ClearValue to revert it back but it stayed as Aquamarine. Looking at this post: ( http://wpf.2000things.com/tag/clearvalue/) told me that ClearValue reverts it back to the last assigned value. So I tried setting the Background property to Nothing and it worked. The label background was now the default color. – tolsen64 Apr 09 '15 at 17:27
15

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
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Beauty
  • 865
  • 11
  • 14
  • 1
    This answer is useful in MVVM environment. There I cannot clear the BorderBrushProperty as in the otherwise good answer by Daniel. But setting a controls´ background to its default brush works by returning the brush explicitly in a property on the viewmodel. – Julian Jul 25 '16 at 15:44
4

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.

dotNET
  • 33,414
  • 24
  • 162
  • 251
0

Does this work? Setting it to black is better than using the ClearValue method

public string ErrorMessage
{
    set
    {
        if (string.IsNullOrEmpty(value))
        {
            _textbox.Background = Brushes.Black;
        }
        else
        {
            _textbox.Background = Brushes.Red;
        }

        _errorMessage.Text = value;
    }
}
Athiwat Chunlakhan
  • 7,589
  • 14
  • 44
  • 72
0

Just store the default settings. Here a code excample.

        System.Windows.Media.Brush save;

        private void Window_Loaded(object sender, RoutedEventArgs e)
                {
          //Store the default background 
        save = testButton.Background;

        }


        private void ChangeBackground(){

        testButton.Background = Brushes.Red;

        }

        private void restoreDefaultBackground(){

        //Restore default Backgroundcolor

        testButton.Background = save;

        }
Andreas
  • 3,843
  • 3
  • 40
  • 53