2009-10-14 19 views
14

私は次のようなコマンドをバインドしています。MVVMでCommandParameter値を受け取る

<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の値を取得する方法は?

ありがとうございます。

答えて

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)); 
+1

ありがとう非常に、その作業は非常にありがとうございます。 もう一度ありがとうございます。 –

+0

とし、 '_nextCommand = new RelayCommand(this.DisplayNextPageRecords);のように使うことができます。 –

関連する問題