C#学习教程:HowtoPassVariablesasCommandParameter该指令如下所示:publicclassEditPersonCommand:ICommand{privatebool_CanExecute=false;publicboolCanExecute(objectparameter){PersonModelp=parameterasPersonModel;CanExecuteProperty=(p!=null)&&(p.Age>0);返回CanExecuteProperty;}公共事件EventHandlerCanExecuteChanged;publicvoidExecute(objectparameter){}privateboolCanExecuteProperty{get{return_CanExecute;}set{if(_CanExecute!=value){_CanExecute=value;事件处理器can_execute=CanExecuteChanged;如果(can_execute!=null){can_execute.Invoke(this,EventArgs.Empty);}}}}}ViewModel看起来像这样:publicclassPersonViewModel:ViewModelBase{privatePersonModel_PersonModel;私人EditPersonCommand_EditPersonCommand;//////必须使用无参数构造函数来满足///publicPersonViewModel():this(newPersonModel()){}publicPersonViewModel(PersonModelpersonModel){_PersonModel=personModel;}公共ICommandEditPersonCommand{get{if(_EditPersonCommand==null){_EditPersonCommand=newEditPersonCommand();}返回_EditPersonCommand;xaml看起来像这样:这也不适用于在调用CanExecute时始终显示为null的对象参数,并且按钮永远不会启用。如果我将CommandParameter值更改为Hello,那么我会在调用CanExecute时收到Hello,因此我不确定为什么该变量不起作用。任何帮助将不胜感激。更新:我也试过为模型创建一个公共属性(我真的不想公开模型,只是想看看它是否有效,但没有成功)。//将其添加到ViewModelpublicPersonModelPersonModelProp{get{return_PersonModel;}设置{_PersonModel=值;OnPropertyChanged("PersonModelProp");}}并将xaml更改为:但仍然没有运气。ViewModel确实实现了INotifyPropertyChangedCommandParameter是否始终为空,或者您是否仅在第一次执行时检查?在这种情况下,您声明属性的顺序似乎很重要,因为设置Command属性会导致CanExecute在设置CommandParame之前立即触发。尝试将CommandParameter属性移到Command属性之前:另外,请参阅此处和此处。编辑为确保正确引发事件,应在PersonModelProp值更改时引发CanExecuteChanged事件。命令:publicclassEditPersonCommand:ICommand{publicboolCanExecute(objectparameter){PersonModelp=parameterasPersonModel;返回p!=null&&p.Age>0;}公共事件EventHandlerCanExecuteChanged;publicvoidExecute(objectparameter){//命令实现}publicvoidRaiseCanExecuteChanged(){varhandler=CanExecuteChanged;如果(处理程序!=null){处理程序(这个,EventArgs.Empty);}}}和视图模型:publicclassPersonViewModel:ViewModelBase{privatePersonModel_PersonModel;私人EditPersonCommand_EditPersonCommand;//////必须使用无参数构造函数来满足///publicPersonViewModel():this(newPersonModel()){_EditPersonCommand=newEditPersonCommand();}publicPersonViewModel(PersonModelpersonModel){_PersonModel=personModel;}publicICommandEditPersonCommand{get{return_EditPersonCommand;}}publicPersonModelPersonModelProp{get{return_PersonModel;}设置{_PersonModel=va卢;OnPropertyChanged("PersonModelProp");EditPersonCommand.RaiseCanExecuteChanged();答案有两点:首先,正如@akton提到的,您只能绑定到公共属性,但它不必是DependencyProperty。其次,我需要弄清楚的是,您必须在Command属性之前设置CommandParameter的绑定。即希望这有帮助:)_PersonModel是私有的,因此不可访问。创建公开属性并将其绑定到CommandParameter的公共属性。请记住使属性成为依赖属性(技术上不是必需的,但它有帮助),ViewModel应该在更改时实现INotifyProperty并触发PropertyChanged事件以便更新绑定。我认为您的EditPersonCommand有问题(它没有触发)。我用relayCommand检查了它,它有效!这是代码:ViewModel:publicclassPersonViewModel:ViewModelBase{privatePersonModel_PersonModel;私有ICommand_EditPersonCommand;//////必须使用无参数构造函数来满足///publicPersonViewModel():this(newPersonModel()){}publicPersonViewModel(PersonModelpersonModel){PersonModelProp=personModel;}publicICommandEditPersonCommand{get{if(_EditPersonCommand==null){_EditPersonCommand=newRelayCommand(ExecuteEditPerson,CanExecuteEditPerson);}返回_EditPersonCommand;{PersonModelp=参数作为PersonModel;return(p!=null)&&(p.Age>0);}privatevoidExecuteEditPerson(objecto){}publicPersonModelPersonModelProp{get{return_PersonModel;}设置{_PersonModel=值;NotifyPropertyChanged("PersonModelProp");}}}而这个RelayCommand(火灾事件没问题!)publicclassRelayCommand:ICommand{#regionConstantsand字段privatereadonlyPredicatecanExecute;私有只读动作执行;#endregion#region构造函数和析构函数publicRelayCommand(Actionexecute):this(execute,null){}publicRelayCommand(Actionexecute,PredicatecanExecute){if(execute==null){thrownewArgumentNullException("execute");}this.execute=执行;this.canExecute=canExecute;}#endregion#regionEventspubliceventEventHandlerCanExecuteChanged{add{CommandManager.RequerySuggested+=value;}移除{CommandManager.RequerySuggested-=值;}}#endregion#region实现接口#regionICommand[DebuggerStepThrough]publicboolCanExecute(objectparameter){returnthis.canExecute==null||this.canExecute(参数);}publicvoidExecute(objectparameter){this.execute(parameter);}#endregion#endregion}Xmal位:以上就是C#学习教程:如何将变量作为CommandParameter传递的全部内容分享给大家。如果对大家有用,需要进一步了解C#学习教程,希望大家关注~本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
