WPF: reverting brush to default/original

前端 未结 5 830
不知归路
不知归路 2021-02-05 03:10

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

相关标签:
5条回答
  • 2021-02-05 03:20

    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
    
    0 讨论(0)
  • 2021-02-05 03:20

    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;
        }
    }
    
    0 讨论(0)
  • 2021-02-05 03:24

    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;
    
            }
    
    0 讨论(0)
  • 2021-02-05 03:33

    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.

    0 讨论(0)
  • 2021-02-05 03:37

    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.

    0 讨论(0)
提交回复
热议问题