我的问题是如何从模型中触发 ViewModel 方法。
我正在使用 MVVM 开发 WPF 应用程序。所以我有一个按钮,,SubmitMedPrescCommand
(使用中继命令实现)和一个SelectedMedPrescRepeat
绑定到模型的组合框()。当用户选择下拉菜单时,会在模型的属性中引发 PropertyChange 事件,但我需要调用 CanExecute(在 ViewModel 中)才能启用按钮。
下面列出了我的代码示例。任何帮助,将不胜感激 !提前致谢 !
视图模型是这样的:
public class EpCreateMedicineViewModel : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public ICommand SubmitMedPrescCommand { get; set; }
public EpCreateMedicineViewModel()
{
SubmitMedPrescCommand = new RelayCommand<MedicinePrescriptionForSubmission>(ExecuteSubmitMedPrescCommand, CanExecuteSubmitMedPrescCommand);
}
private MedicinePrescriptionForSubmission _medicinePrescForSubm;
public MedicinePrescriptionForSubmission MedicinePrescForSubm
{
get { return _medicinePrescForSubm; }
set
{
if (value != this._medicinePrescForSubm)
{
this._medicinePrescForSubm = value;
OnPropertyRaised("MedicinePrescForSubm");
}
}
}
public bool CanExecuteSubmitMedPrescCommand(object parameter)
{
if (_medicinePrescForSubm.MedicineForSubmGeneralInfo.SelectedMedPrescRepeat!=null)
{
return true;
}
else
{
return false;
}
}
}
以及属性所属的模型:
public class MedicinePrescriptionForSubmission
{
public MedicineForSubmGeneralInfo MedicineForSubmGeneralInfo { get; set; }
public class MedicineForSubmGeneralInfo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private MedicinePrescriptionRepeat _selectedMedPrescRepeat; // THE PROPERTY THAT THE COMBOBOX IS BINDED TO
public MedicinePrescriptionRepeat SelectedMedPrescRepeat
{
get { return _selectedMedPrescRepeat; }
set
{
_selectedMedPrescRepeat = value;
OnPropertyRaised("SelectedMedPrescRepeat");
//CanExecuteSubmitMedPrescCommand(_selectedMedPrescRepeat); // THE METHOD OF THE VIEWMODEL THAT I WANT TO BE TRIGERRED WHEN MedicinePrescriptionRepeat changes
}
}
private void OnPropertyRaised(string propertyname)
{
PropertyChangedEventHandler handle = PropertyChanged;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
}
}