0

We can use ICommand instead of Routed Event to implement MVVM, so we can write the logic in ViewModel.

Here is a example:

<Button Width="40" Command="{Binding CommandOne}">Click1
</Button>

But we need to Set CommandParameter all by ourself, and my problem is how to send the RoutedEventArgs as the CommandParameter.

 private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
 {
        throw new NotImplementedException();
 } 

How can I use the RoutedEventArgs as the CommandParameter in my ViewModel? Any ideas?

H.B.
  • 166,899
  • 29
  • 327
  • 400
johnson
  • 388
  • 5
  • 16
  • why would you need RoutedEventArgs in VM? what Data you want to send with your command ? – Nitin Sep 10 '13 at 02:41
  • In WebBrowser, when Navigating i need the navigating url which is in NavigatingCancelEventArgs. – johnson Sep 10 '13 at 02:48
  • Cant you just pass the navigating URL as the CommandParameter, e.g: `CommandParameter="{Binding Source, ElementName=myWebBrowser}"` – sa_ddam213 Sep 10 '13 at 03:11
  • I don't want the source, I want the redirect url during the Navigating procedure. – johnson Sep 10 '13 at 04:24

1 Answers1

1

I don't think there is any straight forward way of doing this (nothing I know). But this can be achieved using interactivity EventTrigger and defining your own TriggerAction to capture the EventArgs and send as CommandParameter.

Here is one example of doing this. Here the example is of different event but can be applied to any.

http://social.msdn.microsoft.com/Forums/silverlight/en-US/5cd586e7-640f-447b-9040-e9270173abf7/passing-drop-event-data-in-a-command-parameter-using-mvvm-and-the-interactivity-framework

Nitin
  • 18,344
  • 2
  • 36
  • 53
  • Thanks, I gone using an attached behavior to doing this. When attached a command to an Routed event, give the RoutedEventArgs as parameter to this command's execute method.Maybe next time I will try the way you tell me. – johnson Sep 10 '13 at 04:27
  • I've test the method you told me, it works. Thank you very much. – johnson Sep 10 '13 at 04:38
  • you might want to post the answer in that case... implementation of attached behaviour... thanks :) – Nitin Sep 10 '13 at 04:38