62

In WPF validation, whats the difference between the following:

ValidatesOnNotifyDataErrors = True

ValidatesOnDataErrors = True

NotifyOnValidationError = True

When should you use these properties correctly in XAML?

Community
  • 1
  • 1
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71

2 Answers2

65

ValidatesOnNotifyDataErrors and ValidatesOnDataErrors are used when you want a XAML bound control to validate its input based on an interface implemented in the ViewModel/Model, for ValidatesOnNotifyDataErrors that interface is INotifyDataErrorInfo and for ValidatesOnDataErrors it is IDataErrorInfo.
for example let's say you have a view model like this:

class PersonViewModel : IDataErrorInfo {

    public string FirstName {get; set;}

    string IDataErrorInfo.Error 
    {
        return string.Empty;
    }

    string IDataErrorInfo.this[string columnName] {
        if (columnName == "FirstName" &&) {
            if (this.FirstName.Length > 20)
                return "FirstName can't be more than 20 characters.";

        }
        return string.Empty;
    }

}

and then in your view you have a textbox that is bound to the FirstName property like this: <TextBox Text={Binding Path=FirstName, ValidatesOnDataErrors=True} /> now if the user entered 20 characters or more in the textbox an error will be detected.

On the other hand NotifyOnValidationError is used when you want an event to be raised when the bound fails validation.

I usually use ValidatesOnDataErrors in my XAML controls for validation and i haven't had a need for the other two, so it depends on your situation.

EDIT: I am updating my answer as I have learned some new things, so I need to make this more relevant.

ValidatesOnDataErrors is used in thick clients, or in other words when the validation is performed on the client side such as a desktop WPF or WinForm application and model objects implement IDataErrorInfo.

On the other hand, ValidatesOnNotifyDataErrors would be a better fit for thin clients (multi-tier applications) such as client-server applications (Silverlight, WPF with WCF, etc ..) where the validation takes place on the server.

This way when the user types something for example in a TextBox, the value is sent to the server asynchronously for validation, and when validation results come back an event is raised (ErrorsChanged event to be exact), then the view picks that up and displays it using the appropriate method, of course in this case the model would implement INotifyDataErrorInfo.

Dan
  • 1,130
  • 2
  • 20
  • 38
Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
  • 1
    Another thing to note ... is that INotifyDataErrorInfo is only available in .NET 4.5 and forward ... and if you are stuck supporting Windows XP (and therefore you can't use .NET 4.5), then you are stuck with IDataErrorInfo instead of INotifyDataErrorInfo. – cplotts Aug 10 '16 at 22:48
  • Can I have the `INotifyDataErrorInfo` implemented in the VM whilst the model is a POCO provided via a property on the VM? – Shimmy Weitzhandler Dec 05 '19 at 08:26
  • 1
    Hi @Shimmy, I honestly haven't worked with anything WPF for several years now so cannot really help you. – Ibrahim Najjar Dec 05 '19 at 19:53
  • 1
    Thanks. I came up with a different solution, posted it [here](https://stackoverflow.com/a/59203094/75500) for the reference. – Shimmy Weitzhandler Dec 05 '19 at 21:00
8

Just for your info: IDataErrorInfo.Error is not used in WPF and can return null or throw a NotImplementedException.
This property was used in WinForms.

Personally, I prefer to use INotifyDataErrorInfo because it allows to have multiple error messages mapped to a single property.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
thomasgalliker
  • 1,279
  • 13
  • 19
  • it seems to show errors in real time when idataerrorinfo wait you lose focus on the control you want to test. – Vonkel. Nov 23 '20 at 16:14
  • 2
    @Vonkel. To trigger the validation, the source must be updated. Therefore the time of the validation occurring depends on the current value of UpdateSourceTrigger property. So setting it to PropertyChanged makes the validation fire immediately after any change to the bound property. – Lubo Apr 18 '21 at 13:18