Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  


How to implement a reusable ICommand

Introduction

If you are using the MVVM (model-view-viewmodel) pattern, one of the most used mechanism to bind actions to the view are commands. To provide a command, you have to implement the ICommand interface. This is quite simple, but you have to do it over and over again. This is cumbersome.

The idea of this pattern build an universal command, that takes two delegates: One that is called, when ICommand.Execute(object param) is invoked, and one that evalues the state of the command when ICommand.CanExecute(object param) is called.

In addition to this, we need a method, that triggers the CanExecuteChanged event. This causes the ui element to reevaluate the CanExecute() of the commmand.

Sample implementation

 
public class DelegateCommand : ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;
 
    public event EventHandler CanExecuteChanged;
 
    public DelegateCommand(Action<object> execute) 
                   : this(execute, null)
    {
    }
 
    public DelegateCommand(Action<object> execute, 
                   Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }
 
    public override bool CanExecute(object parameter)
    {
        if (_canExecute == null)
        {
            return true;
        }
 
        return _canExecute(parameter);
    }
 
    public override void Execute(object parameter)
    {
        _execute(parameter);
    }
 
    public void RaiseCanExecuteChanged()
    {
        if( CanExecuteChanged != null )
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }
}
 
 
 
 




Last modified: 2010-02-22 22:57:52
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
lan
Commented on 19.April 2010
Good artical!
If with an example is better
YJ
Commented on 12.May 2010
If you want an example, check http://www.codeproject.com/KB/WPF/MVVMForDummies.aspx
A lot simple, but it could help.
Ganesh
Commented on 20.August 2010
Good artical!

but no any example need eample for all property as like your wcf article
C-MOSE
Commented on 23.August 2010
I apprieciate your feedback but I cant explain it anymore in depth because I just copy and pasted this from another website
Javaman
Commented on 28.September 2010
This one the delegate command doesn't really make sense to me... here's why, if in a view we want to decouple a Buttom command to a Command Object why would we delegate it (in some cases) back to view logic? Or is it that this command is a mediator to delegate to the implementation of the command in another class?
Henke
Commented on 12.October 2010
=> Javaman, I guess you shouldnt go back to the view, usecase for Delegate/RelayCommands is in my world to directly connect the command to the viewmodel. Without it you will have to delegate it in the codebehind instead.
dct
Commented on 23.November 2010
Hi,
if you would like to use this class with WPF4 you have to remove the override keywords.
Take care
Hitesh
Commented on 24.November 2010
Hi,
Good article. Override keyword not needed.
Just curious to know how is RaiseCanExecuteChanged() method called or when is it called.
Thanks

Sushil
Commented on 28.December 2010
Hi ,good representation but ovveride is not needed
Catalin Manoliu
Commented on 10.January 2011
Hi Christian,

A better example I suggest is this RelayCommand:
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030
The implementation of ICommand.CanExecuteChanged is relying on the wpf's CommandManager.Requery suggested event. This will improve binding scenarios.

Regards,
Catalin
costas...
Commented on 9.March 2011
Hi
the override bool CanExecute and the override void Execute has no override...
ali
Commented on 11.September 2011
THANK YOU FOR CODE OF DELEGATECOMMAND.

Name
E-Mail (optional)
Comment