Let's have a button Command
property bound to a custom command.
When should I implement ICommand
and when derive from RoutedCommand
? I see that RoutedCommand implements ICommand.
In which case could I need to iplement an ICommand
?
What about MVVM model? Which one suits better for this purpose?
The only thing I would add to Rich McGuire's answer is that RoutedCommands (and their more prevalent descendant RoutedUICommand have to be wired up with event handlers to work correctly.
Most MVVM implementations I have come across attempt to leverage binding against the ViewModel and thus the ViewModel (and not the View) owns the CanExecute/Execute logic.
In contrast, the event handlers move that burden to the View. The handling can then be propagated to the ViewModel, but this means a slightly higher degree of coupling between ViewModel and View (casting + method call, etc.).
As you have noticed the
RoutedCommand
class is an implementation of theICommand
interface, its main distinction if that its function is similar to that of aRoutedEvent
:In a case when you don't want the behavior of the
RoutedCommand
you'll be looking at your own implementation ofICommand
. As for the MVVM pattern I can't say that one solution, it seems that everyone has their own methodology. However, here are a few approaches to this problem that I've come across: