-2

i'm currently working on a project using MVVM and Entity Framework Code First Model. So i implemented IDataErrorInfo and INotifyPropertyChanged in my model classes. Now, in my viewmodel i implemented a SaveCommand and a CanSave boolean method, my question is how can i raise canexecutechanged for the whole entity and not individual properties? since my properties already implemented InotifyPropertyChanged in model.

This is my Model Class

 public class Guest:ValidatableBindableClass
{
    //my properties here
    //implement InotifyPropertyChanged and IDataErrorInfo
}

This is my ViewModelClass:

 public class AddEditGuestViewModel:BindableClass
{
    private Hospede guest;
    public RelayCommand SaveCommand  { get; set; }
    private readonly Hmsdb.HMS context = new Hmsdb.HMS();

    public Hospede Guest
    {
        get { return guest; }
        set { SetProperty(ref guest, value, propertyName: "Guest"); }
    }

    private void OnSave()
    {

        context.Hospedes.Add(Guest);
        context.SaveChanges();
    }

    private bool CanSave()
    {
        return context.Entry(Guest)
            .GetValidationResult().IsValid;
    }
}
Gonguela
  • 25
  • 5
  • 1
    Don't post images of code. Copy the relevant code into the question and format accordingly. Just posting images reduces the searchability of the question. – Lithium Apr 06 '17 at 10:18

1 Answers1

0

You can raise the PropertyChanged event for all properties by passing null or string.Empty to the constructor of the PropertyChangedEventArgs:

PropertyChanged(this, new PropertyChangedEventArgs(null));

You will need to call the RaiseCanExecuteChanged() method of each RelayCommand though.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Depending on the command implementation, a call to `CommandManager.InvalidateRequerySuggested()` might be enough.. – grek40 Apr 06 '17 at 10:26
  • It is pretty ineffective though: http://stackoverflow.com/questions/1751966/commandmanager-invalidaterequerysuggested-isnt-fast-enough-what-can-i-do. And the entity seems to have only one command anyway. – mm8 Apr 06 '17 at 10:28
  • My model class already implements propertychanged event for each property, the question is how can i rise canexecute changed on viewmodel class sinse i´m binding to guest entity and not Gues.SomeProperty? Thanks for replay... – Gonguela Apr 06 '17 at 10:41
  • Raise CanExecuteChanged from where? – mm8 Apr 06 '17 at 10:42
  • @mm8 from ViewModel – Gonguela Apr 06 '17 at 10:45
  • @mm8 Guest is the Entity Model Implementing INotifyPropertyChanged and IDataErrorInfo not the ViewmModel – Gonguela Apr 06 '17 at 11:01
  • What are you talking about? Again: from where (which class) are trying to raise the event? – mm8 Apr 06 '17 at 11:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/140064/discussion-between-gonguela-and-mm8). – Gonguela Apr 06 '17 at 11:17