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 を作成するときに、その「実行」ラムダがパラメーターを受け取る方法を確認してください。それをメソッドに渡します。

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