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


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)
Edit: To see this in action, check out my MVVM Example App
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


    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?


    • 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!!

  6. Paul Gibson
    March 23, 2015 at 10:40 pm

    I am implementing an ICommand interface with a DelegateCommand class that is essentially the same as yours. I want to bind a button to a command (successful) that also passes a reference to the window so that the viewmodel can close the window. But in the view model what worked is:

    public ICommand ButtonClick
    get { return new DelegateCommand(ButtonAction); } //<—ERROR HERE

    When I add the parameter to ButtonAction . . . .

    public void ButtonAction(Window w)

    I get compiler errors on the line marked: The best overloaded method match for DelegateCommand (constructor) has some invalid arguments. I don't understand the syntax I think but cant figure what I'm doing wrong. In xaml I am using the CommandParameter for the button to specify the window. How do I deal with it in the DelegateCommand class and the viewmodel?

    • Grant Crofton
      March 24, 2015 at 1:58 pm

      The problem here I think is that an Action (which is what we need to pass to the DelegateCommand constructor) represents a function that takes no parameters, but you’re passing it a function that takes a Window parameter.

      This guy seems to have a solution for what you’re trying to do: http://blogs.microsoft.co.il/shimmy/2011/10/05/close-window-from-viewmodel/

  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
  5. November 14, 2015 at 9:37 am
  6. August 12, 2019 at 7:53 pm

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: