IDataErrorInfo on Model classes and enabling the Save button

May 28, 2012 at 12:04 PM

Hi

I am trying to figure out how to use IDataErrorInfo in a WPF application with SimpleMVVM.

I have implemented IDataErrorInfo on my model classes. And so far everything is working as expected. My validation rules are triggered when I tab from field to field on a WPF form and I set the style on the field to indicate a validation error.

The problem I have is that I want to bind the Save button (using DelegateCommand here) so that the Save button is enabled when the model is valid.

I have tried to create a property called IsValid on the model object and I have confirmed that this is being set correctly. I have tried listening for the INotifyPropertyChanged event on this property but I never receive it. I believe this is because I am calling BeginEdit.

I have something like the following

this.Model.PropertyChanged += (s,e) =>
{
    this.Save.RaiseCanExecuteChanged();
}

then in another method called after this

this.BeginEdit();

I suspect that since BeginEdit is making a clone of my model object, my changes and validations are happening on the clone but I have my property changed subscription on the original model object, so I never receive the property changed notifications and the RaiseCanExecuteChanged method is never called.

Is there any way around this? 

thanks

Chris

Jun 1, 2012 at 6:21 PM

chrdunne,

I am trying to implement Validation in my WPF application with IDataNotifyError and SimpleMVVM as well.  Would you be able to post any code examples for how you are currently doing this?  I would appreciate it very much.

Thanks.

Scott