2009-10-14 13 views
14

sto vincolanti mio comando come:Ricezione CommandParameter Value in MVVM

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

Qui, ho anche impegnare i suoi beni CommandParameter, ora come per andare a prendere il suo valore da nextCommand.

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

La sua definizione di funzione:

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; 

    } 

Come per recuperare il valore CommandParameter?

Grazie in anticipo.

risposta

33

Modifica della definizione di metodo:

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

Vedere come quando si crea il tuo RelayCommand, la sua "Esegui" lambda prende un parametro? Passa al tuo metodo:

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

Grazie mille, funziona molto. Grazie ancora. –

+0

e quindi è possibile utilizzare come '_nextCommand = new RelayCommand (this.DisplayNextPageRecords);' –