2009-10-14 11 views
14

Estoy vinculantes mi comando como:Recibir CommandParameter Valor en MVVM

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

Aquí, también, ato su propiedad CommandParameter, ahora la forma de buscar a su valor de NextCommand.

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

Su definición de función:

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; 

    } 

¿Cómo se ha podido recuperar el valor CommandParameter?

Gracias de antemano.

Respuesta

33

Cambiar su definición del método:

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

Vea cómo al crear su RelayCommand, su "Ejecutar" lambda toma un parámetro? Pase eso a su método:

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

Muchas gracias, está trabajando mucho. Gracias de nuevo. –

+0

y luego puede usar como '_nextCommand = new RelayCommand (this.DisplayNextPageRecords);' –