2017-04-06 112 views
-2

我目前正在使用MVVM和实体框架代码优先模型的项目。所以我在我的模型类中实现了IDataErrorInfo和INotifyPropertyChanged。现在,在我的viewmodel中,我实现了一个SaveCommand和一个CanSave布尔方法,我的问题是如何为整个实体提高canexecutechanged而不是单独的属性?因为我的属性已经在模型中实现了InotifyPropertyChanged。使用实体框架MVVM验证

这是我的模型类

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

这是我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; 
    } 
} 
+1

请勿发布代码图片。将相关代码复制到相应的问题和格式中。只是发布图像会降低问题的可搜索性。 – Lithium

回答

0

你可以通过nullstring.EmptyPropertyChangedEventArgs的构造提高PropertyChanged事件的所有属性:

PropertyChanged(this, new PropertyChangedEventArgs(null)); 

但是,您需要调用每个RelayCommandRaiseCanExecuteChanged()方法。

+0

根据命令实现,对'CommandManager.InvalidateRequerySuggested()'的调用可能已经足够了。 – grek40

+0

虽然这是非常无效的:http://stackoverflow.com/questions/1751966/commandmanager-invalidaterequerysuggested-isnt-fast-enough -我能做什么。无论如何,这个实体似乎只有一个命令。 – mm8

+0

我的模型类已经为每个属性实现了属性changed事件,问题是我怎么才能提出canexecute改变viewmodel类sinse我绑定到访客实体,而不是Gues.SomeProperty?感谢您的重播... – Gonguela