INotifyDataErrorInfo WPF implementation

ChummyChum

Member
Joined
Sep 28, 2022
Messages
10
Programming Experience
1-3
Data validation in WPF seems too complicated, I am slowly getting the hang of it, but still need some help from you guys when using INotifyDataErrorInfo.

So, my problem is that I don't want my validation occur on property change, that is why I've set it up that all my property checks are done when a user clicks 'Save' button.

When the button is clicked I check all my properties and do 'AddError()' if something is not right. It works, the screen lights up in red, error messages appear etc.

BUT I am stuck on how to raise ErrorChanged for all of the properties when I clear it.

So when a user clicks 'Save' button again I want to clear all of the errors dictionary.clear() and then raise the errorschanged event BUT for all of the properties.

In all of the tutorials about INotifyDataErrorInfo there is:

C#:
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
and I get that but I want to raise Errors changed for the whole form, dont want to raise individually.

Is something like that possible? Personally I am a fan of form validation on user click and not on property changed.

I hope I described my ask clearly :)
 
That's what I thought would happen as well, but as I discovered it does not. That's why my test code above goes and clears the errors on fields that are valid.

What is interesting, though, is if your bind to the entire view model, passing in a null property name for the ErrorsChanged event will actually call GetErrors() passing in null once, and then an empty string the second time. The issue though was returning an empty list did not clear all errors, and returning a non-empty list did not set errors on all.

Thinking about this more, it makes sense. If you had to implement this validation systems using INotifyDataError, how would you know which controls to mark as having errors or not having errors when you don't have a property name? How will you deal with UI elements that are dynamically added? Best to just let the programmer to deal with it.
 
Back
Top Bottom