Search code examples
wpfvalidationidataerrorinfo

How to validate a different property that the one changed using IDataErrorInfo with WPF?


I'm using IDataErrorInfo to validate my viewmodels as it allows me to use a clear xaml sintax and it's pretty straightforward once you get it.

My question is how to validate one property when another one changes because the validation does not consist only on the value on the cell but depends on others. I see that the validation method is only called when the value of the property changes.

Thanks in advance.


Solution

  • with IDataErrorInfo you can hook into

    public string this[string columnName]
    

    by overriding it, or writing your own.

    in your case do a check that the columnName equals the property you are looking to validate and then either return a string with a value to represent a validation error, or null to say there was no error.

    Also, to make sure it gets revalidated, when the dependent properties get updated, make sure you do a OnPropertyChanged for the main property