I was wondering what is the best validation that I can use in my case. I read a lot about raising validation on properties throwing exception, rule validators and implementibg interfaces but I al getting confused. I need a method that will not throw exception while I init all the properties with null because it stops my init code (it can be while creating a new object), on the other side I need to show all the errors for empty mandatory fields when the form is first open or if the user type and remove letters. I implemented some interface on all my objects and can call to a full validate on init/before save or a property validation when a specific validation is changed. But how can I add the ErrorControlTemplate for all the controls that are Binding to those properties? what is the best approach for handling the validation on the code behing ajd updating the GUI accordingly
CodePudding user response:
The best approach for handling data validation in WPF is to implement the INotifyDataErrorInfo inerface in a view model.
Please refer to this article for an example implementation.
In short, you could store your validation error messages in a Dictionary<string, string> or Dictionary<string, ICollection<string>> field in the view model class, depending on whether you intend to support one or several validation errors per property, and then raise the ErrorsChanged event whenever you want the UI to refresh to reflect your current validation status.
