WPF: revirtiendo el pincel a predeterminado / original


Soy un completo novato en WPF.

En este momento estoy haciendo un usercontrol para elementos de formulario llamado "LabeledTextbox" que contiene una etiqueta, un cuadro de texto y un bloque de texto para mensajes errorm.

Cuando el código using agrega un mensaje errorm, quiero poner el borde del cuadro de texto en rojo. Pero, cuando el errormessage se elimina, me gustaría volver al bordercolor predeterminado del cuadro de texto. Creo que debe haber una manera muy fácil de hacer esto.

Mi código:

(en 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;
    }
}
Author: Thomas Stock, 2009-08-20

5 answers

Podrías usar

_textBox.ClearValue(TextBox.BorderBrushProperty);

Que eliminará el valor asignado directamente y volverá al valor definido por el estilo o la plantilla.

 44
Author: Daniel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-20 14:20:29

Puede obtener los colores predeterminados de la clase SystemColors

Aquí está la lista de todos los colores del sistema: http://msdn.microsoft.com/de-de/library/system.windows.systemcolors.aspx

Predeterminado color de fondo del área del cliente:

     _textbox.Background = SystemColors.WindowBrush;

Predeterminado color del texto dentro del área del cliente:

     _textbox.SystemColors.WindowTextBrush
 10
Author: Beauty,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-30 19:35:53

Puede que llegue tarde a la fiesta, pero para futuros lectores, también puede usar Button.BackgroundProperty.DefaultMetadata.DefaultValue para este propósito. Esto es especialmente útil cuando está utilizando un convertidor donde necesita devolver un valor y, por lo tanto, no puede usar ClearValue() call.

 3
Author: dotNET,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-20 05:14:45

¿Funciona esto? Configurarlo en negro es mejor que usar el método ClearValue

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

        _errorMessage.Text = value;
    }
}
 0
Author: Athiwat Chunlakhan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-20 14:24:59

Simplemente almacene la configuración predeterminada. Aquí un ejemplo de código.

        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
Author: Andreas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-30 12:08:14