MVVM

2009-10-14 21 views
14
में CommandParameter मूल्य प्राप्त

मैं तरह मेरे आदेश बाध्यकारी हूँ:MVVM

<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) जैसे उपयोग कर सकते हैं;' –