Search code examples
c#wpfinotifydataerrorinfo

When is INotifyDataErrorInfo GetErrors() Called?


The documentation I've found for WPF's INotifyDataErrorInfo seems to state that the GetErrors() method is called when either a value is set through the UI, or as a result of the INotifyDataErrorInfo's ErrorsChanged event having been called.

But that doesn't match what I'm seeing when debugging a window whose DataContext is a viewmodel that implements INotifyDataErrorInfo (the viewmodel in question is built on Mvvm Light).

Instead, GetErrors() is called when the window is initialized and its DataContext is set, but afterwards only when the viewmodel code raises the ErrorsChanged event. Which in my case it does whenever certain properties are set in the viewmodel, by code I've written to raise the ErrorsChanged event.

I'm asking this question because I want to know if I need to handle the situation where GetErrors() might get called by WPF without me first having called the validation code. That could erroneously report that no errors exist, when in fact that's only the case because validation hasn't taken place yet.


Solution

  • It's called when errorschanged is raised.

    I just experimented with a sample I have. This thing: https://gallery.technet.microsoft.com/scriptcenter/WPF-Entity-Framework-MVVM-78cdc204

    I put a break point in the geterrors method of my base class and spun it up.
    Geterrors was not called until I made something invalid.
    If you see different behaviour then it's something you are doing differently.