views:

461

answers:

2

Hi,

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;
    }
}
+3  A: 

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
great, thanks!*adds dependencyproperty research to the to-do list*
Thomas Stock
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!
Justin
A: 

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;
    }
}
Jonathan Shepherd
oh sorry, I made a mistake in my post. It doesn't because the default is a gradient color.
Thomas Stock
@Thomas Stock :0 Never tested it. So also sorry.
Jonathan Shepherd