14 parameters wpf mvvm commandbinding
我绑定我的命令,如:
<Button Command="{Binding NextCommand}"
CommandParameter="Hello"
Content="Next" />
Run Code Online (Sandbox Code Playgroud)
在这里,我还绑定了它的CommandParameter属性,现在如何从NextCommand获取它的值.
public ICommand NextCommand
{
get
{
if (_nextCommand == null)
{
_nextCommand = new RelayCommand(
param => this.DisplayNextPageRecords()
//param => true
);
}
return _nextCommand;
}
}
Run Code Online (Sandbox Code Playgroud)
其功能定义:
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;
}
Run Code Online (Sandbox Code Playgroud)
如何获取CommandParameter值?
提前致谢.
Mat*_*ton 33
更改方法定义:
public ObservableCollection<PhonesViewModel> DisplayNextPageRecords(object o)
{
// the method's parameter "o" now contains "Hello"
PageNumber++;
CreatePhones();
return this.AllPhones;
}
Run Code Online (Sandbox Code Playgroud)
看看在创建RelayCommand时,它的"执行"lambda是如何获取参数的?将其传递给您的方法:
_nextCommand = new RelayCommand(param => this.DisplayNextPageRecords(param));
Run Code Online (Sandbox Code Playgroud)