0

I've got a wpf form where validation works fine for single-field validation. I've got the DataAnnotations classes wired up to the IDataErrorInfo class so that they display properly.

Now I've got a more complicated form with more complicated validation. I've implemented IValidatableObject on the view model, and I see that this is called. I manually call Validator.TryValidateObject any time a field changes, and I can see that the IDataErrorInfo class properly returns errors.

My problem is that I don't know how to get the UI to recheck it's validation status. For example, I've got fields A, B, and C that are only required if D is a certain value. So there's no DataAnnotation flags on those fields -- I just return the list of errors in the Validate implementation of IValidatableObject.

Now when I edit D, no error is displayed in A,B, or C. If I put something in A, then take it back out, then A will show the error. But I want all the fields to do it automatically. Is there a normal way to do this, and in particular, trigger it from the ViewModel?

Clyde
  • 8,017
  • 11
  • 56
  • 87

1 Answers1

1

I had the same question a while back and posted it on SO here

What I ended up doing was creating an Extension method that hooked into the PropertyChanging event and raising the validation error for any properties within the specified ValidationGroup

It is used like:

public MyViewModel()
{
    this.AddValidationGroup(
        new List<string> { "A", "B", "C", "D" },
        GetValidationError, OnPropertyChanged);
}

The downside is your class needs to implement INotifyPropertyChanging (I was using Entity Framework, and the entities automatically implement that interface)

There is also another answer posted there which doesn't use the PropertyChanging event, and may help you.

And of course, there's always the solution of simply raising OnPropertyChanged("X"); inside the set method of your related properties.

public string A
{
    get { return _a; }
    set
    {
        _a = value;
        RaisePropertyChanged("A");
        RaisePropertyChanged("B");
        RaisePropertyChanged("C");
        RaisePropertyChanged("D");
    }
}
Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • I actually just did the last -- they weren't EF objects, and it was a lot simpler than I thought – Clyde Oct 24 '11 at 18:55