WPF data binding - set NotifyOnValidationError to true for all bindings with validation rules

In my WPF application, I want to set NotifyOnValidationError to true (the framework defaults to false) for all child controls / bindings if they have any ValidationRules attached to the binding. Indeed, it would be nice to specify other default values ​​for the binding - for example. ValidatesOnDataErrors must also always be true.

For example, in the following text box, I do not want to manually specify the NotifyOnValidationError property.

 <TextBox> <TextBox.Text> <Binding Path="PostalCode" ValidatesOnDataErrors="True" NotifyOnValidationError="True"> <Binding.ValidationRules> <rules:PostalCodeRule /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> 
+7
source share
2 answers

Since Binding is just a markup extension, you can create a custom markup extension that extends Binding and sets these properties to their default values.

+5
source

Following the answer of Ragepato.
The easiest way to do this is to create your own Binding , which inherits from Binding , and then set the things you need, for example NotifyOnValidationError="True" and ValidatesOnDataErrors="True" in the constructor.

 public class ExBinding : Binding { public ExBinding() { NotifyOnValidationError = true; ValidatesOnDataErrors = true; } } 

And then instead you use this binding

 <TextBox> <TextBox.Text> <local:ExBinding Path="PostalCode"> <local:ExBinding.ValidationRules> <rules:PostalCodeRule /> </local:ExBinding.ValidationRules> </local:ExBinding> </TextBox.Text> </TextBox> 
+7
source

All Articles