我目前正在使用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;
}
}
請勿發佈代碼圖片。將相關代碼複製到相應的問題和格式中。只是發佈圖像會降低問題的可搜索性。 – Lithium