Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is INotifyDataErrorInfo.GetErrors called with null vs String.empty?

In the msdn page for InotifyDataErrorInfo.GetErrors it says that GetErrors method is called with a parameter which can be either:

  • The name of the property we want to retrieve error info for
  • Null
  • String.Empty

The documentation doesn't say when this method is called with null vs String.Empty. I've seen both cases in my app and I need to understand when I can expect one or the other.

Clarification: I'm not asking about how I should implement GetErrors method (I simply test for both null and empty). The question is more to understand why the WPF framework tries to call this method sometimes with null and sometimes with an empty string (I encountered both cases in my app). If the intent is to ask for errors that are not tied to a specific property, why use two different values for the call when only one is enough?

like image 904
disklosr Avatar asked Jan 25 '16 12:01

disklosr


1 Answers

WPF calls InotifyDataErrorInfo.GetErrors(null/string.Empty) to get errors of "entire view model". All controls that have data context or binding to view model with "entire view model errors" will be rendered with error template. For example you have view model Credentials with two properties: UserName, Password. You may implement something like this:

IEnumerable InotifyDataErrorInfo.GetErrors(string propertyName)
    {
        if (UserNames.Length == 0)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return "Some credentials component is wrong.";
            }
            else if (propertyName == "UserNames")
            {
                return "User name is required field.";
           }
        }
    }

However in most cases you may return null when string.IsNullOrEmpty(propertyName).

like image 105
Oleksandr Bilyk Avatar answered Oct 05 '22 01:10

Oleksandr Bilyk