Implementing INotifyDataErrorInfo on an ObservableCollection of objects

589 views Asked by At

Context

I'm developing a WPF application using MVVM and Entity Framework (database first). I have an ObservableCollection of Airframe objects (being viewed through a CollectionView so that I can handle next / previous logic). I have a text box which allows one of the Airframe properties for the currently viewed Airframe to be edited and I'm currently setting up the validation for this. The approach I'm taking is to implement INotifyDataErrorInfo using the example shown in WPF 4.5: Validating Data in Using the INotifyDataErrorInfo Interface.

Issue

In the example in that article, validation is fired via the setter of an individual property defined in the ViewModel like this:

private string _username;
public string Username
{
    get { return _username; }
    set
    {
        _username = value;
        ValidateUsername(_username);
    }
}

Question

How can I fire the validation in my particular context? As I'm using Entity Framework (database first) I can't fiddle with setters in the generated Airframe model. I can't see how I would get granular access to change a setter within the ObservableCollection of Airframe objects. Nor can I think of any other place from which the validation could be triggered.

1

There are 1 answers

0
bradgonesurfing On

You can inject INPC support to your generated airframe model using

https://github.com/Fody/PropertyChanged

using PropertyChanged;

[ImplementPropertyChanged]
public partial class AirFrame
{
}

and then listen for the event raised INotifyPropertyChanged and use that as a trigger to force validation.