Luis Font
Luis Font

Reputation: 70

How to get an existing property from style

I'm developing a WPF project where I need to validate textboxes on code-behind and then change the border color of those textboxes to some unknown color defined by the user configuration; the problem is that to overwrite the MouseOver effect I would need to set the value from XAML directly via ControlTemplate or Style.

Is there a way to get the current value assigned to the TextBox from XAML? Maybe binding it to itself?

I will appreciate any help you can give.

Upvotes: 1

Views: 53

Answers (1)

Adi Lester
Adi Lester

Reputation: 25211

When validating a TextBox, or any control for that matter, it's common to use an adorner to display the validation error (in your case, setting the border color). You can have a look at an example here.

Using this method, you don't actually change the TextBox's border, but create a new visual layer on top of the TextBox that draws the new border around it. This is pretty much the way validations should be done in WPF, and it also solves your problem of having to deal with changing the TextBox's border value back and forth.

Upvotes: 1

Related Questions