52

I'm confused about command pattern. There are so many different explanations about the commands. I thought the code below was delegatecommand, but after reading about the relaycommand, I am in doubt.

What is the difference between relaycommand, delegatecommand and routedcommand. Is it possible to show in examples that have relevance to my posted code?

class FindProductCommand : ICommand
{
    ProductViewModel _avm;

    public FindProductCommand(ProductViewModel avm)
    {
        _avm = avm;
    }

    public bool CanExecute(object parameter)
    {
        return _avm.CanFindProduct();
    }

    public void Execute(object parameter)
    {
        _avm.FindProduct();
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

}
Zaz
  • 1,074
  • 3
  • 17
  • 29
  • have you done a google search.. look at the examples here try the code and apply it to what you have.. http://msdn.microsoft.com/en-us/library/ff654132.aspx http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.mvvm.relaycommand.aspx http://msdn.microsoft.com/en-us/library/system.windows.input.routedcommand.aspx – MethodMan Jan 06 '13 at 09:06
  • 2
    Yes, it did not help so much – Zaz Jan 06 '13 at 09:09
  • I would suggest doing a google search on command pattern's on google then – MethodMan Jan 06 '13 at 09:12
  • 11
    @DJKRAZE: i have done it, that is why i'm being doubt with my posted code, is it delegatecommand or relaycommand? There are many different explanations. – Zaz Jan 06 '13 at 09:17

1 Answers1

58

Your FindProductCommand class implements the ICommand interface, which means it can be used as a WPF command. It is neither a DelegateCommand nor a RelayCommand, nor is it a RoutedCommand, which are other implementations of the ICommand interface.


FindProductCommand vs DelegateCommand/RelayCommand

Generally, when an implementation of ICommand is named DelegateCommand or RelayCommand, the intention is that you don't have to write a class that implements the ICommand interface; rather, you pass the necessary methods as parameters to the DelegateCommand / RelayCommand constructor.

For example, instead of your entire class, you could write:

ProductViewModel _avm;
var FindPoductCommand = new DelegateCommand<object>(
    parameter => _avm.FindProduct(),
    parameter => _avm.CanFindProduct()
);

(Another, perhaps greater benefit than the savings in boilerplate code -- if you instantiate the DelegateCommand / RelayCommand within your viewmodel, your command has access to the internal state of that viewmodel.)

Some implementations of DelegateCommand / RelayCommand:

Related:


FindProductCommand vs RoutedCommand

Your FindProductCommand will execute FindProduct when triggered.

WPF's built-in RoutedCommand does something else: it raises a routed event which can be handled by other objects in the visual tree. This means you can attach a command binding to those other objects to execute FindProduct, while attaching the RoutedCommand itself specifically to one or more objects that trigger the command, e.g. a button, a menu item, or a context menu item.

Some related SO answers:

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136