There is an unsaved comment in progress. You will lose your changes if you continue. Are you sure you want to reopen the work item?

26

Closed

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.

comments

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:

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:

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:

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.

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.