Search code examples
c#wpfxamlinotifydataerrorinfo

How to suppress INotifyDataErrorInfo validation for a Property


I have a class implements INotifyDataErrorInfo I have some properties with Error Notification. for example,

public class Request : INotifyPropertyChanged, INotifyDataErrorInfo
{
    public string LineOfBusinessIdentifier
    {
        get { return  lineOfBusinessIdentifier; }
        set 
        { 
            lineOfBusinessIdentifier = value;
            ValidateLineOfBusiness();
            NotifyPropertyChanged();
        }
    }
      // ValidateLineOfBusiness() Implementation for validation.
}

This class is inherited by plenty of other classes. All works fine. Now i have one place i dont want to show the notification in the UI for a particular operation, and need the notification after the operation. Is there anyway i can suppress the notification.


Solution

  • if ValidatesOnNotifyDataErrors (introduced since .Net 4.5) is set to false, the binding doesn't check and reports errors. Default value is true

    "{Binding Path=LineOfBusinessIdentifier, ValidatesOnNotifyDataErrors=False}"
    

    also possible to erase Validation.ErrorTemplate (to hide error notification in view) and reset it in trigger setter

    <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
    
    <!--need custom error template-->
    <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorTemplate}""/>
    

    this doesn't disable validation, only hides visual indicator of error


    viewModel derived from Request can override GetErrors method (if it virtual) and disable LineOfBusinessIdentifier property notification under some conditions:

    pseudo-code:

    override GetErrors(string propertyName) 
    {
        if (someCondition)
           return base.GetErrors().Where(prop != LineOfBusinessIdentifier)
        else
           return base.GetErrors()
    }