WPF: reverting brush to default/original

ε祈祈猫儿з 提交于 2019-12-03 01:09: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.

Beauty

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

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.

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

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;

        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!