Search code examples
wpfmvvmcommandcommand-pattern

What is the accepted pattern for WPF commanding in MVVM?


I'm working on a WPF app and I understand the command pattern pretty well, but I've found that there are several different implementations of the command pattern for MVVM. There's Josh Smith's implementation in his WPF sample app, the DelegateCommand from Prism, and the CommandBindings implementation.

My question is, what is the generally accepted best practice for using commands with MVVM? My application uses Prism so DelegateCommand is available to us.

The devs on my team are arguing about which approach is "best." Some don't like the numerous .cs files generated for each command, others prefer that everything be wired up via CommandBindings. I'm at a loss. Can anyone shed some light?


Solution

  • Two points to consider:

    The commands provided by different MVVM frameworks, such as the CommandSinkCommand or SimpleCommand (from Cinch), etc. work as normal ICommands do. In particular, the CanExecute handler is evaluated whenever the WPF believes something has happened which could cause an UI change. You can also manually force this through CommandManager.InvalidateRequerySuggested().

    In contrast to that, the DelegateCommands<> of Prism do not call the CanExecute handler unless you manually call RaiseCanExecuteChanged() on the command. If you wish to change this so it also gets fired when the commands are normally requeried, see the code at http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=47338

    EDIT:

    Second point: The commands of MVVM frameworks usually accept an object as command parameter. In contrast, the DelegateCommands<> of Prism are more strongly typed (though you could of course have a DelegateCommands if you want to).