14

我正在绑定我的命令,例如:

<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 值?

提前致谢。

4

1 回答 1

33

更改您的方法定义:

public ObservableCollection<PhonesViewModel> DisplayNextPageRecords(object o)
{
    // the method's parameter "o" now contains "Hello"
    PageNumber++;
    CreatePhones();
    return this.AllPhones;
}

看看当你创建你的 RelayCommand 时,它的“Execute” lambda 是如何接受一个参数的?将其传递到您的方法中:

_nextCommand = new RelayCommand(param => this.DisplayNextPageRecords(param));
于 2009-10-14T09:04:10.893 回答