Получение значения CommandParameter в MVVM
Я связываю свою команду как:
<Button Command="{Binding NextCommand}"
CommandParameter="Hello"
Content="Next" />
Здесь я также связываю его свойство 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?
Спасибо заранее.
Ответы
Ответ 1
Измените определение метода:
public ObservableCollection<PhonesViewModel> DisplayNextPageRecords(object o)
{
// the method parameter "o" now contains "Hello"
PageNumber++;
CreatePhones();
return this.AllPhones;
}
Посмотрите, как при создании вашего RelayCommand его "Execute" лямбда принимает параметр? Передайте это в свой метод:
_nextCommand = new RelayCommand(param => this.DisplayNextPageRecords(param));