Search code examples
wpfvalidationpropagation

WPF : Propagate a validation error down the visual tree


I often use and re-use usercontrols in my apps.

For example, if a user should enter an integer number, I have a usercontrol named "IntegerEditor", which has a label for the title, a textbox for the user input, and a label for the units (inches, seconds, etc...).

My user control already has a validation rule that I called "IntegerValidationRule", which control if the user input is an integer.

I have a style associated with that validation rule that change the control background and add the error message in a tool tip.

Sometimes, I use these kind of basic controls in my UI, but I want to add other validation rules, such as "The integer must be greater than 1023", with special error messages as "UDP Port under 1024 are reserved, please choose a higher number".

Do you know any way that I could add these validations rules somewhere else but that it would still trigger the style on my control? I thought about propagating the validation error down the visual tree, but found nothing great about that on the net.

Thank you for yours answers,

A.Jeanrichard


Solution

  • Well... you could respond to routed "validation failed" events, but that'd be more difficult IMO than having an application-level ViewModel that's responsible for coordinating validation between different ViewModels.