How to automatically set ValidatesOnExceptions, ValidatesOnDataErrors and NotifyOnValidationError in all TextBoxes?

Currently, I'm creating textboxes and specifying ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True every time I create a binding:

<TextBox Width="150" Text="{Binding Expander1Name, ElementName=Window, 
         Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, 
         ValidatesOnExceptions=True, ValidatesOnDataErrors=True, 
         NotifyOnValidationError=True}" Background="#FFF5EECC"></TextBox>   

I would like to set ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True with a global XAML setter.

Can anyone here give a definitive answer whether it is possible to do, if not - why, if yes - how?

Answers


Not possible in the way you describe, since these properties are present on the Binding instances within the DependencyObjects rather than being exposed as properties on the DependencyObjects themselves. It is not possible to create a Style that targets Bindings.

That said, there are a couple of ways you could work around this:

  1. Define your own class that extends Binding and sets whatever defaults you want. You would then use that class everywhere you've currently used Binding.
  2. Define an attached behavior that finds all Bindings within the object to which it is attached and sets the defaults you want.

The first option would be preferable to me, since the second could have unforeseen consequences (what if there's a binding where you don't want it to take on the defaults?). The second option would also be much slower, since it would need to dynamically search through all dependency properties of the item to which it is attached.


I asked almost the same question some time before maybe you can have a look also there:

Style For Bindings?


Need Your Help

CreateMutex -- "Access is denied"

c++ winapi

The following code works without any problem sometimes and at other times it throws "Access is denied" error. The behavior is not consistent.

How do I disable the WPF WebBrowser control's click noise?

.net wpf browser

I've got a simple little WPF app with a TextBox and a WebBrowser control. As I type into the TextBox the WebBrowser updates with its content.