2009-10-14 10 views
14

다음과 같이 명령을 바인딩합니다.MVVM에서 CommandParameter 값 받기

<Button Command="{Binding NextCommand}" 
    CommandParameter="Hello" 
    Content="Next" /> 

여기에서 CommandCarameter 속성을 바인딩하여 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);와 같이 사용할 수 있습니다. –

관련 문제