Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

So in this particular MVVM implementation I'm doing, I need several commands. I really got tired of implementing the ICommand classes one by one, so I came up with a solution, but I don't know how good it is, so the input of any WPF expert here will be greatly appreciated. And if you could provide a better solution, even better.

What I did is a single ICommand class and two delegates which take an object as a parameter, one delegate is void (for OnExecute), the other bool (for OnCanExecute). So in the constructor of my ICommand (which is called by the ViewModel class) I send the two methods, and on each ICommand method I invoke the delegates' methods.

It works really good, but I'm not sure if this is a bad way to do it, or if there's a better way. Below is the complete code, any input will be greatly appreciated, even negative, but please be constructive.

ViewModel:

public class TestViewModel : DependencyObject
    public ICommand Command1 { get; set; }
    public ICommand Command2 { get; set; }
    public ICommand Command3 { get; set; }
    public TestViewModel()
        this.Command1 = new TestCommand(ExecuteCommand1, CanExecuteCommand1);
        this.Command2 = new TestCommand(ExecuteCommand2, CanExecuteCommand2);
        this.Command3 = new TestCommand(ExecuteCommand3, CanExecuteCommand3);
    public bool CanExecuteCommand1(object parameter)
        return true;
    public void ExecuteCommand1(object parameter)
        MessageBox.Show("Executing command 1");
    public bool CanExecuteCommand2(object parameter)
        return true;
    public void ExecuteCommand2(object parameter)
        MessageBox.Show("Executing command 2");
    public bool CanExecuteCommand3(object parameter)
        return true;
    public void ExecuteCommand3(object parameter)
        MessageBox.Show("Executing command 3");

ICommand:

public class TestCommand : ICommand
    public delegate void ICommandOnExecute(object parameter);
    public delegate bool ICommandOnCanExecute(object parameter);
    private ICommandOnExecute _execute;
    private ICommandOnCanExecute _canExecute;
    public TestCommand(ICommandOnExecute onExecuteMethod, ICommandOnCanExecute onCanExecuteMethod)
        _execute = onExecuteMethod;
        _canExecute = onCanExecuteMethod;
    #region ICommand Members
    public event EventHandler CanExecuteChanged
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    public bool CanExecute(object parameter)
        return _canExecute.Invoke(parameter);
    public void Execute(object parameter)
        _execute.Invoke(parameter);
    #endregion
                Check out Karl Shifflet's implementation of RelayCommand: codeproject.com/KB/WPF/ExploringWPFMVVM.aspx
– Jarrett Meyer
                Sep 24, 2009 at 0:26

This is almost identical to how Karl Shifflet demonstrated a RelayCommand, where Execute fires a predetermined Action<T>. A top-notch solution, if you ask me.

public class RelayCommand : ICommand
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;
    public RelayCommand(Predicate<object> canExecute, Action<object> execute)
        _canExecute = canExecute;
        _execute = execute;
    public event EventHandler CanExecuteChanged
        add => CommandManager.RequerySuggested += value;
        remove => CommandManager.RequerySuggested -= value;
    public bool CanExecute(object parameter)
        return _canExecute(parameter);
    public void Execute(object parameter)
        _execute(parameter);

This could then be used as...

public class MyViewModel
    private ICommand _doSomething;
    public ICommand DoSomethingCommand
            if (_doSomething == null)
                _doSomething = new RelayCommand(
                    p => this.CanDoSomething,
                    p => this.DoSomeImportantMethod());
            return _doSomething;

Read more:
Josh Smith (introducer of RelayCommand): Patterns - WPF Apps With The MVVM Design Pattern

It does look similar to mine. It'd be interesting to know the pros and cons of using this. Do you have a link to the article or blog where you read this? – Carlo Sep 23, 2009 at 22:26 im using it as well, the only con i can find is that you dont have a keyboard shortcut assigned to the command. any ideas? – Aran Mulholland Sep 24, 2009 at 7:14 @Aran The best solution I've found for assigning keyboard shortcuts to a RelayCommand is CommandReference. The only con to it is that you don't get the auto population of the shortcut in context menus and such. joyfulwpf.blogspot.com/2009/05/… – dustyburwell Sep 24, 2009 at 14:05

I have written this article about the ICommand interface.

The idea - creating a universal command that takes two delegates: one is called when ICommand.Execute (object param) is invoked, the second checks the status of whether you can execute the command (ICommand.CanExecute (object param)).

Requires the method to switching event CanExecuteChanged. It is called from the user interface elements for switching the state CanExecute() command.

public class ModelCommand : ICommand
    #region Constructors
    public ModelCommand(Action<object> execute)
        : this(execute, null) { }
    public ModelCommand(Action<object> execute, Predicate<object> canExecute)
        _execute = execute;
        _canExecute = canExecute;
    #endregion
    #region ICommand Members
    public event EventHandler CanExecuteChanged;
    public bool CanExecute(object parameter)
        return _canExecute != null ? _canExecute(parameter) : true;
    public void Execute(object parameter)
        if (_execute != null)
            _execute(parameter);
    public void OnCanExecuteChanged()
        CanExecuteChanged(this, EventArgs.Empty);
    #endregion
    private readonly Action<object> _execute = null;
    private readonly Predicate<object> _canExecute = null;

I've just created a little example showing how to implement commands in convention over configuration style. However it requires Reflection.Emit() to be available. The supporting code may seem a little weird but once written it can be used many times.

Teaser:

public class SampleViewModel: BaseViewModelStub
    public string Name { get; set; }
    [UiCommand]
    public void HelloWorld()
        MessageBox.Show("Hello World!");
    [UiCommand]
    public void Print()
        MessageBox.Show(String.Concat("Hello, ", Name, "!"), "SampleViewModel");
    public bool CanPrint()
        return !String.IsNullOrEmpty(Name);

UPDATE: now there seem to exist some libraries like http://www.codeproject.com/Articles/101881/Executing-Command-Logic-in-a-View-Model that solve the problem of ICommand boilerplate code.

@Carlo I really like your implementation of this, but I wanted to share my version and how to use it in my ViewModel

First implement ICommand

public class Command : ICommand
    public delegate void ICommandOnExecute();
    public delegate bool ICommandOnCanExecute();
    private ICommandOnExecute _execute;
    private ICommandOnCanExecute _canExecute;
    public Command(ICommandOnExecute onExecuteMethod, ICommandOnCanExecute onCanExecuteMethod = null)
        _execute = onExecuteMethod;
        _canExecute = onCanExecuteMethod;
    #region ICommand Members
    public event EventHandler CanExecuteChanged
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    public bool CanExecute(object parameter)
        return _canExecute?.Invoke() ?? true;
    public void Execute(object parameter)
        _execute?.Invoke();
    #endregion

Notice I have removed the parameter from ICommandOnExecute and ICommandOnCanExecute and added a null to the constructor

Then to use in the ViewModel

public Command CommandToRun_WithCheck
        return new Command(() =>
            // Code to run
        }, () =>
            // Code to check to see if we can run 
            // Return true or false
public Command CommandToRun_NoCheck
        return new Command(() =>
            // Code to run

I just find this way cleaner as I don't need to assign variables and then instantiate, it all done in one go.

Thanks for sharing this! It's definitely interesting to see other ways to solve this. Ever since I read about the RelayCommand, I decided to adopt that pattern. I haven't done WPF in YEARS, but I definitely used the RelayCommand for a few years before the trend in my company shifted to web. – Carlo Nov 8, 2017 at 22:18

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.