5

I've been seeing tutorials where people are creating the methods like CanExecute in their code. I'm assuming they are doing this to help the reader understand how it all works. When I look up Command and ICommand it takes me to the ICommand class on MSDN that is used for Windows Store apps. Is there not a Command class for WPF?

Pallas
  • 1,499
  • 5
  • 25
  • 57

1 Answers1

8

The built-in implementation of ICommand in WPF is RoutedCommand (and its sibling RoutedUICommand). RoutedCommand works like this:

The Execute and CanExecute methods on a RoutedCommand do not contain the application logic for the command as is the case with a typical ICommand, but rather, these methods raise events that traverse the element tree looking for an object with a CommandBinding. The event handlers attached to the CommandBinding contain the command logic.

The problem with this is that these event handlers must be attached to the code-behind for your view, which is exactly what you do not want to do in MVVM.

Tutorials where you see CanExecute methods in code (and by that we really mean code outside the ICommand implementation) are using custom command implementations such as DelegateCommand and RelayCommand which are designed to "forward" their CanExecute/Execute logic to functions provided on the fly; typically, those are methods on the viewmodel that exposes the command.

These implementations are usually provided by MVVM frameworks (for these two examples the frameworks are Prism and MVVM Light respectively), but they are really simple (both are open source, grab the code and read it) and there's nothing stopping you from copy/pasting the code if you don't want the whole of the framework.

You could summarize the above as "there is built-in a command class in WPF, but it's not really useful in the context of MVVM".

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 1
    It should probably be noted, depending on how new OP is to MVVM, that you can implement your own version of DelegateCommand very simply without having to use a specific MVVM framework. – Thelonias Mar 06 '13 at 13:54
  • @Ryan: Thanks, I finished some edits to the answer and it now reflects your suggestion. – Jon Mar 06 '13 at 13:59
  • So DelegateCommand is provided by Microsoft for me to use? This is both my first WPF application and my first MVVM application. Although I have been using WinForms for a couple of years. So far I really like WPF! Because this is my first MVVM application, I'd rather create all of the code myself and not use any Frameworks like MVVM Light so I can have a better understanding of how everything works. – Pallas Mar 06 '13 at 23:32
  • @RandomlyKnighted: Yes, you can go to the [Prism project site](http://compositewpf.codeplex.com/), grab the source (do check the license first if you are going to distribute the application) and use it. Keep in mind that MVVM frameworks are largely open source, which can help you even more than writing the code yourself (you can still see how things work, and you get to observe why they decided to do things the way they did by first-hand experience). – Jon Mar 07 '13 at 10:01
  • @Jon would DelegateCommand work with .NET 3.0? I'm targeting my application toward that framework as 3/4 of the computers we work on do not have the newest frameworks when we first work on them. (Of course when we are done they have .NET Framework 4 installed.) – Pallas Mar 07 '13 at 13:24
  • @RandomlyKnighted: It should, IIRC there's nothing fancy inside the implementation. But why don't you try to compile in order to get a definitive answer? – Jon Mar 07 '13 at 13:31
  • Yep, DelegateCommand works with .NET 3.0. Thanks for your help! – Pallas Mar 07 '13 at 22:26