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.


Need Your Help

AngularJS - dynamic link broken in Google Chrome Extension

javascript google-chrome angularjs google-chrome-extension

I have a problem with link in sample chrome extensions I've been working on.

Conditional Printing with XSLT

xml xslt printing conditional

Once again, apologies if this is a dumb question... my current situation is that I'm having to do some work in XSLT and I really don't know a lot about it. I'm learning, but I still need help

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.