Home > WPF > Simplified MVVM Commanding with DelegateCommand

Simplified MVVM Commanding with DelegateCommand

The MVVM (Model-Miew-ViewModel) pattern for WPF works really well, but for me has one big disadvantage – the sheer verbosity of it.  There’s just so much code to type!  Especially when you’re going for the ‘pure’ approach of no code in your code-behind.

One of the worst offenders for this is the Command model, whereby in your ViewModel you define a class which implements ICommand.  This is made available to the View via a property, and when called, calls some method in your ViewModel.

(Note:  This only applies to WPF.  Silverlight doesn’t currently support commands in the same way.)

Here’s an example, from inside my ViewModel (MeasurementViewModel):

(This is set up in the XAML as follows:)

You can see the custom ICommand (AddDefinitionCommand), a local variable to hold it, and a property getter for the view to find it.  Now considering that this doesn’t even have the code which performs the action (which is viewModel.AddMeasurementDefinition), that’s lot of overhead.  And you have to do that for every single command!  That’s button clicks, selection changes, and so on.

One of the problems is that the Command model provides a lot of flexibility by defining the CanExecute() method and CanExecuteChanged event, but I don’t need those in my simple scenario.

So, how to write a bit less code?  Of course, you could make some good use of Code Snippets, but there are better solutions.  The best one I’ve found is the DelegateCommand approach used in Microsoft’s Composite Application Library (a.k.a Prism), also discussed in Josh Smith’s MVVM article (where he calls it a RelayCommand).  This involves defining an ICommand which uses delegates, and means you have less boilerplate code to write.

As I’m not interested in using the full features of the ICommand, my simple DelegateCommand goes like this:

DelegateCommand class definition

DelegateCommand.cs

And to call it, you simply need the following:

You can see the local member for the DelegateCommand, the ViewModel constructor where we define the method to call, and the property to make it available to the View.  Much simpler, I’m sure you’ll agree!

Of course, if you want to include the rest of the Command functionality, you can just extend the DelegateCommand.  So there you have it, all of your MVVM problems solved!  Well, nearly..

The Code

(As I don’t seem to be able to upload DelegateCommand.cs for you to download, here’s the text for you to copy & paste.  I’ll work WordPress out one of these days…)

using System;
using System.Windows.Input;
namespace CommandUtils
{
public class DelegateCommand: ICommand
{
private Action _executeMethod;
public DelegateCommand(Action executeMethod)
{
_executeMethod = executeMethod;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_executeMethod.Invoke();
}
}
}
Edit: To see this in action, check out my MVVM Example App
About these ads
Categories: WPF Tags: , ,
  1. Yaizd
    August 24, 2010 at 10:22 am

    Very Good article, the DelegateCommand class never uses the event CanExecuteChanged!

  2. relentlessdevelopment
    August 26, 2010 at 3:41 pm

    @Yaizd, well spotted, it doesn’t use the CanExecuteChanged event – and the CanExecute() method isn’t used in any meaningful way either. This event (and method) has to be there to satisfy the ICommand interface, but in this example I wanted a simple solution to performing an action based on an input, and have no use for this.

    I don’t like leaving unused code in there just to implement an interface, but when working with a framework like WPF sometimes you just have to go with what it wants to do.

  3. November 11, 2010 at 2:32 am

    Greetings,

    I have a inquiry for the webmaster/admin here at relentlessdevelopment.wordpress.com.

    May I use part of the information from this blog post above if I give a link back to your site?

    Thanks,
    Harry

    • relentlessdevelopment
      November 17, 2010 at 9:17 am

      Sure, go for it.

  4. Rohit
    April 2, 2013 at 4:28 pm

    Superb article!! 1 of my favorite

  5. Divyans Mahansaria
    April 7, 2014 at 4:22 pm

    Very nice article! Thank you!!

  1. April 1, 2010 at 6:25 am
  2. June 8, 2010 at 9:14 am
  3. January 23, 2013 at 3:33 pm
  4. January 5, 2014 at 2:15 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 247 other followers

%d bloggers like this: