MVVM Fabric: Using ActionCommand

The first thing I am going to discuss in my series describing my new WPF library, MVVM Fabric, is ActionCommand.  ActionCommand is an implementation of the ICommand interface.  The ICommand interface is interesting because it can be used in WPF and Silverlight as a way to route certain events (such as clicks) from the view to the view model.

Now, there’s nothing stopping you from doing custom implementations of the ICommand interface for every command your application needs.  I found that to be overkill, however, because in every case I just wanted to turn around and call some method when the command was executed.  Enter ActionCommand.

ActionCommand provides a way to create commands that will call methods on your view model when executed.  MVVM Fabric provides two flavors of ActionCommand, a plain one and a generic one.

Plain ActionCommand

The plain ActionCommand is great for those situations where you just want to execute some method on your view model and have no need for a parameter.  It’s usage is quite simple.

The above example has a method called Save which needs to be called from the view.  So, an ICommand property, called SaveCommand, is exposed on the view model.  In the view model’s constructor, SaveCommand is instantiated with an instance of ActionCommand.  The plain ActionCommand takes a plain Action as its parameter.  A plain Action is a method that has no parameters and a void return type.

To leverage this command, we need to wire up something like a Button to it.  With our view model already defined as the DataContext, the following XAML will do that for us.

Now, when the button is clicked our Save method will be executed on the view model.

Generic ActionCommand

The generic flavor of ActionCommand is useful for those situations where you want to execute some method but also need a parameter to be passed in.  WPF allows you to bind a CommandParameter along with the Command.  The generic part of the generic ActionCommand defines what type that CommandParameter is.  The usage is a little more complicated than the plain ActionCommand, but not by much.

As you can see, the generic ActionCommand uses an Action which takes a parameter type as defined by the generic parameter.  The view side looks a little different too.

Notice that we now have a binding set up on the CommandParameter property.  That binding tells WPF what to pass to the Command as a parameter, which ultimately ends up passed to the SelectMovie method. 

NOTE: If you are looking at the sample application included with MVVM Fabric, you’ll notice that the binding expression I am referencing is more complex.  That is due to the fact that the binding occurs in a DataTemplate, which has a Movie as it’s DataContext, the the MovieCommand lives on the view model.  I used some binding-fu to bind to two different DataContexts.

Conclusion

So, there we have it.  Commanding is not the ultimate solution to wiring events from the view to the view model, but it does work well for some cases.  When commanding applies, ActionCommand is a clean and simple way to leverage it.

Share

1 thought on “MVVM Fabric: Using ActionCommand”

Comments are closed.