0

I am using MVVM Light toolkit for my WPF application and I would like to know if its possible, when using EventToCommand, to pass multiple parameters to RelayCommand and Is it possible to pass properties of EventArgs instead of passing the whole EventArgs ?

Regards, Nabeel

nabeelfarid
  • 4,156
  • 5
  • 42
  • 60
  • The EventToCommand only has a single command parameter, but you could take a look at the source to see how the attached property is defined and add more parameters. – Matt Casto Aug 23 '10 at 12:44
  • Why would you want to pass properties of the EventArgs if you can pass the whole EventArg param? Please expand on your scenario and explain what you're trying to accomplish. – Matt Casto Aug 23 '10 at 12:45
  • Hi Matt,I do not want to pass EventArgs because I don't want my viewmodel to depend upon various derivatives of Eventargs (e.g. keyupeventargs, keydowneventargs) for different event types. I just want to pass the data/properties that I need from EventArgs to my viewmodel method. Its more cleaner i think. – nabeelfarid Sep 08 '10 at 13:55

2 Answers2

3

what if the scenario is

 <i:Interaction.Triggers>
                <i:EventTrigger EventName="KeyDown">
                    <cmd:EventToCommand Command="{Binding SearchKey}" PassEventArgsToCommand="True" 
                    CommandParameter="{Binding Text, ElementName=TextSearchCashDrawer}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>

what is on enter key press i need to read the text from the textbox and perform search.

        SearchKey=new RelayCommand<KeyEventArgs>(e=>
                                                     {
                                                         if(e.PlatformKeyCode==13) //enter key
                                                         {


                                                         }
                                                     });

using this I can filter which key has been pressed but how to get that parameter if enter key is pressed in this mvvmlight.

webKite
  • 285
  • 3
  • 15
  • you can bind the Textbox value to another property on your viewmodel and use that property in your relay command – nabeelfarid Sep 08 '10 at 13:55
2

If all you want to do is capture the enter key press, you can create a KeyBinding via InputBinding. The following example in XAML would capture the Enter key press in the TextBox and the Command, FindCommand in this case, would handle it in your ViewModel.

<TextBox Width="80">
     <TextBox.InputBindings>
          <KeyBinding Key="Enter" Command="{Binding FindCommand}" />
     </TextBox.InputBindings>
</TextBox>

Worked for me!

Andy
  • 91
  • 2