26
Vote

Event to Command

description

Hello,
 
Could you please support Event to Command in Prism? There are a few numbers of MVVM frameworks that support EventToCommand. Even we can copy the code or repeat the same thing in our own way, It would be the best if we have official support from Prism Framework.
 
One suggestion that I like to suggest for implementing Event to Command is that VM should have the reference to control. For example: If I'm using third-party control in View, but my view model shouldn't aware of that third-party control. The max scope of this third-party control should be only in View and attached property (not in ViewModel).
 
We will need a lot of tricks when it comes to usability. WPF strong binding is great and what Prism supports so far is also very good for developing WPF/Silverlight business application. But there are some scenarios that we need events (this is where Event to Command comes into picture.), setting focus or handling mouse or etc.
 
Thanks,
Michael Sync

comments

weitzhandler wrote Jan 9, 2012 at 11:25 AM

Very compelling.

aadami wrote Mar 8, 2012 at 5:58 PM

Hi,

For those interested in this subject, you might find useful to know that in order to achieve event-to-command functionality, it’s possible to use Expression Blend’s EventTrigger and InvokeCommandAction like this:
 <TextBox>
      <i:Interaction.Triggers>
           <i:EventTrigger EventName="KeyUp">
                <i:InvokeCommandAction Command="{Binding MyCommand}" />
           </i:EventTrigger>
      </i:Interaction.Triggers>
 </TextBox>  
However, Blend’s built-in InvokeCommandAction does not update the enabled state of the control if the ICommand changes it, which might be useful in certain scenarios. To support this kind of scenarios, since Prism 4.1 release (http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=28950), a custom InvokeCommandAction is provided, which can be used in the same way as Blend’s, but it also updates the control's enabled state appropriately:
 <TextBox>
      <i:Interaction.Triggers>
           <i:EventTrigger EventName="KeyUp">
                <prism:InvokeCommandAction Command="{Binding MyCommand}" />
           </i:EventTrigger>
      </i:Interaction.Triggers>
 </TextBox>  
Regards,

Agustin Adami
http://blogs.southworks.net/aadami

albertod wrote Jun 3, 2012 at 3:46 PM

Very useful.
Is it possible to pass to "MyCommand" a sort of EventArgs argument, to allow the command to know which key has been pressed?

aadami wrote Jun 5, 2012 at 4:51 PM

Hi,

Based on my understanding, currently the InvokeCommandAction provided with Prism, uses the CommandParameter specified in the CommandParameterProperty for command invocation, this can be seen in the following lines of the InvokeCommandAction class:

// This parameter is ignored; the CommandParameter specified in the CommandParameterProperty is used for command invocation.
protected override void Invoke(object unusedParameter)
(...)

Hence, if you need to retrieve the EventArgs and pass them to the command through the CommandParameter property, I believe you might find the following blog post useful:

Silverlight Commands Hacks: Passing EventArgs as CommandParameter to DelegateCommand triggered by EventTrigger
http://weblogs.asp.net/alexeyzakharov/archive/2010/03/24/silverlight-commands-hacks-passing-eventargs-as-commandparameter-to-delegatecommand-triggered-by-eventtrigger.aspx

I hope you find this handy,

Agustin Adami
http://blogs.southworks.net/aadami

vikram30capri wrote Jul 20, 2012 at 9:20 AM

MVVM LightTool KIt EventToCommand behavior, bind any event of any UI element to an ICommand, for example on the ViewModel, directly in XAML. This makes using Commands much easier, without writing code behind. One can even get the EventArgs of the fired event directly in the ViewModel to handle it. Please include this cool feature in PRISM, else developrs have to write a lot of code to handle these scenarios.

aadami wrote Oct 23, 2012 at 4:46 PM

Hi,

For those interested in using the Prism's InvokeCommandAction, take into account that this implementation is located in the Microsoft.Practices.Prism.Interactivity assembly and a reference to this assembly will have to be added to the corresponding project and Xaml file in order to use it (e.g xmlns:prism="clr-namespace:Microsoft.Practices.Prism.Interactivity;assembly=Microsoft.Practices.Prism.Interactivity").

Note, that currently this is not mentioned in the Prism's documentation.

Regards,

Agustin Adami
http://blogs.southworks.net/aadami