2009-10-14 9 views
14

Je suis obligatoire ma commande comme:Réception CommandParameter Valeur en MVVM

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

Ici, je lie également sa propriété CommandParameter, maintenant comment récupérer sa valeur de nextCommand.

public ICommand NextCommand 
    { 
     get 
     { 
      if (_nextCommand == null) 
      { 
       _nextCommand = new RelayCommand(
        param => this.DisplayNextPageRecords() 
        //param => true 
        ); 
      } 
      return _nextCommand; 
     } 
    } 

Sa définition de la fonction:

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; 

    } 

Comment récupérer la valeur CommandParameter?

Merci d'avance.

Répondre

33

Modifier votre définition de la méthode:

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

Voyez comment lorsque vous créez votre RelayCommand, son lambda "Exécuter" prend un paramètre? Transmettez cela dans votre méthode:

_nextCommand = new RelayCommand(param => this.DisplayNextPageRecords(param)); 
+1

Merci beaucoup, beaucoup de travail. Merci encore. –

+0

et ensuite vous pouvez utiliser comme _nextCommand = new RelayCommand (this.DisplayNextPageRecords); ' –