C# 基于实体框架的MVVM验证
我目前正在从事一个使用MVVM和实体框架代码优先模型的项目。因此,我在模型类中实现了IDataErrorInfo和INotifyPropertyChanged。现在,在我的viewmodel中,我实现了一个SaveCommand和一个CanSave boolean方法,我的问题是如何为整个实体而不是单个属性生成canexecutechanged?因为我的属性已经在模型中实现了InotifyPropertyChanged 这是我的模型课C# 基于实体框架的MVVM验证,c#,wpf,entity-framework,mvvm,C#,Wpf,Entity Framework,Mvvm,我目前正在从事一个使用MVVM和实体框架代码优先模型的项目。因此,我在模型类中实现了IDataErrorInfo和INotifyPropertyChanged。现在,在我的viewmodel中,我实现了一个SaveCommand和一个CanSave boolean方法,我的问题是如何为整个实体而不是单个属性生成canexecutechanged?因为我的属性已经在模型中实现了InotifyPropertyChanged 这是我的模型课 public class Guest:Validatabl
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;
}
}
通过将
null
或string.Empty
传递给PropertyChangedEventArgs
的构造函数,可以引发所有属性的PropertyChanged
事件:
PropertyChanged(this, new PropertyChangedEventArgs(null));
不过,您需要调用每个
RelayCommand
的raisecannexecutechanged()
方法。不要发布代码图像。将相关代码复制到问题中并相应设置格式。仅发布图像会降低问题的可搜索性。根据命令的实现,调用CommandManager.invalidateRequestSuggested()
可能就足够了。但这非常无效:。实体似乎只有一个命令。我的模型类已经为每个属性实现了propertychanged事件,问题是既然我绑定的是guest实体而不是Gues.SomeProperty,我如何才能在viewmodel类上执行changed?感谢您的重播…提升CanExecuteChanged from where?@mm8 from ViewModel