我像这样绑定(bind)我的命令:
<Button Command="{Binding NextCommand}"
CommandParameter="Hello"
Content="Next" />
在这里,我还绑定(bind)了它的CommandParameter属性,现在介绍如何从NextCommand获取其值。
public ICommand NextCommand
{
get
{
if (_nextCommand == null)
{
_nextCommand = new RelayCommand(
param => this.DisplayNextPageRecords()
//param => true
);
}
return _nextCommand;
}
}
其功能定义:
public ObservableCollection<PhonesViewModel> DisplayNextPageRecords()
{
//How to fetch CommandParameter value which is set by
//value "Hello" at xaml. I want here "Hello"
PageNumber++;
CreatePhones();
return this.AllPhones;
}
如何获取CommandParameter值?
提前致谢。
最佳答案
更改您的方法定义:
public ObservableCollection<PhonesViewModel> DisplayNextPageRecords(object o)
{
// the method's parameter "o" now contains "Hello"
PageNumber++;
CreatePhones();
return this.AllPhones;
}
看看如何在创建RelayCommand时,其“执行” lambda接受参数?将其传递给您的方法:
_nextCommand = new RelayCommand(param => this.DisplayNextPageRecords(param));