How to get a ValidateAll to trigger the adorners

Feb 6, 2014 at 1:00 PM
In a WPF forms app, I have MVVMValidation working against a number of fields so that any user interaction with a control triggers validation and applies an a simple Validation.ErrorTemplate when the value found to be valid.
The problems is when executing the Validator.ValidateAll() the rules file and the expected validation messages are returned, however the controls are not notified so that they can be adorned. Is there a way to trigger the UI notifications so that validation can be performed from code

XAML
<TextBox Text="{Binding Xxxxxxx, Mode=TwoWay, ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource ValidationTemplate}" />
VM
private string _Xxxxxxx= string.Empty;
public string Xxxxxxx
{
   get { return _Xxxxxxx; }
   set
      {
         if (Set<string>(() => Xxxxxxx, ref _Xxxxxxx, value))
              Validator.Validate(() => Xxxxxxx);
      }
}
Coordinator
Feb 6, 2014 at 2:57 PM
Does your view model implement IDataErrorInfo?

The problem is that the IDataErrorInfo interface doesn't offer a way to notify the UI about changes in validation results. For that reason there is a new interface INotifyDataErrorInfo, but it was introduced in .NET 4.5.

If you use earlier versions of .NET then you can workaround this problem by raising the PropertyChanged event when validation result for a property changes. There is an example of how to do this in the sample available with the source code that you can download here: https://mvvmvalidation.codeplex.com/downloads/get/311274. Take a look at the ValidatableViewModelBase class. It is a partial class and in the ValidatableViewModelBase.Desktop.cs file there is the following code:
public partial class ValidatableViewModelBase : IDataErrorInfo
{
    partial void OnCreated()
    {
        HookUpValidationNotification();
    }

    public string this[string columnName]
    {
        get { return DataErrorInfoAdapter[columnName]; }
    }

    public string Error
    {
        get { return DataErrorInfoAdapter.Error; }
    }

    private void HookUpValidationNotification()
    {
        // Due to limitation of IDataErrorInfo, in WPF we need to explicitly indicated that something has changed
        // about the property in order for the framework to look for errors for the property.
        Validator.ResultChanged += (o, e) =>
        {
            var propertyName = e.Target as string;

            if (!string.IsNullOrEmpty(propertyName))
            {
                RaisePropertyChanged(propertyName);
            }
        };
    }
}