The power of ICommand - Rudi Grobler
Thursday, March 05, 2009 3:08 PM rudi

The power of ICommand

“ICommand contains methods to execute commands. A command can be executed many times, and the parameter values can vary. This interface is mandatory on commands.”

The native support for the command pattern in WPF is great! It allows us to create very decoupled applications and in some instances even remove the need for a code-behind file completely.

WPF implement RoutedCommands & RoutedUICommands… Both of these are great for most scenarios but it still ties you to the visual tree. To completely decouple yourself from the visual tree, we need to explorer some custom implementations of the ICommand interface…

To test these great implementations, I created a application with a VERY basic “ViewModel”

This CustomerViewModel has 2 properties (Name & Surname). It also exposes a ICommand called Clean. If a View calls the Clean command, the name and surname should be set to string.empty! I know this is a overly simplified example but it shows how a View can communicate with the ViewModel in a completely decoupled way

Marlon Grech - SimpleCommand

Marlon created a excellent implementation called SimpleCommand. This truly lives up to its name… here is a example of how I would implement the Clean command using his implementation

Clean = new SimpleCommand()
{
    CanExecuteDelegate = x => Name != string.Empty || Surname != string.Empty,
    ExecuteDelegate = x => Name = Surname = string.Empty
};

Josh SmithRelayCommand

“RelayCommand allows you to inject the command's logic via delegates passed into its constructor. This approach allows for terse, concise command implementation in ViewModel classes. RelayCommand is a simplified variation of the DelegateCommand found in the .”

Josh also has a similar implementation

Clean = new RelayCommand(
    x => Name = Surname = string.Empty,
    x => Name != string.Empty || Surname != string.Empty
        ); 

PRISM team - DelegateCommand<T>

“The DelegateCommand allows delegating the commanding logic instead of requiring a handler in the code behind. It uses a delegate as the method of invoking a target handling method.”

Clean = new DelegateCommand<object>(
    x => Name = Surname = string.Empty,
    x => Name != string.Empty || Surname != string.Empty
        );

What I like about the PRISM team’s implementation is that it supports generics. It did however look like the DelegateCommand<T> did not fire its CanExecuteChanged event. From both Marlon & Josh’s implementations it looks thy are suppose to call CommandManager.RequerySuggested… but I am not sure (It might also be the IActiveAware stuff)

Conclusion

All three these implementations have two thing in common… Thy make it extremely easy to decouple your View and you ViewModel and thy encapsulate the command logic very cleanly!

If you found this article useful, please kick it on DotNetKicks.com

Must read articles about commands

Frameworks

History

Filed under: , , , , ,

Comments

# The power of ICommand

Thursday, March 05, 2009 3:15 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# Dew Drop - March 5, 2009 | Alvin Ashcraft's Morning Dew

Thursday, March 05, 2009 4:06 PM by Dew Drop - March 5, 2009 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - March 5, 2009 | Alvin Ashcraft's Morning Dew

# The power of ICommand - Rudi Grobler

Thursday, March 05, 2009 10:55 PM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# Reflective Perspective - Chris Alcock &raquo; The Morning Brew #301

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #301

# re: The power of ICommand

Friday, March 06, 2009 11:24 AM by James Chaldecott

I love DelegateCommand<T> (and ICommand), but note that one of the problems is that the standard WPF controls don't re-check CanExecute(param) when the CommandParameter changes.

I've posted the code I used to fix this (using an AttachedBehavior) here:

compositewpf.codeplex.com/.../View.aspx

# re: The power of ICommand

Monday, March 09, 2009 11:03 PM by Rishi

Hey I have another implementation of ICommand, more so for Silverlight but it adds a couple of powerful features. Have a look, thanks.

www.orkpad.com/.../I-Command-Silverlight.aspx

# re: The power of ICommand

Tuesday, March 10, 2009 7:15 PM by Christopher Bennage

If you were to add a Caliburn example, then Clean would simply be method. You could decorate it with an attribute (Preview) that points to another method (perhaps named CanExecuteClean).  You don't need to wrap the method in a command.

In the xaml, you might do something like this:

<Button Content="Clean" Message.Attach="Clean" />

# #.think.in infoDose #20 (2nd Mar - 6th Mar)

Tuesday, March 10, 2009 11:44 PM by #.think.in

#.think.in infoDose #20 (2nd Mar - 6th Mar)

# re: The power of ICommand

Thursday, March 12, 2009 8:25 AM by rudi

Thank you Christopher & Rishi for the examples... will add them!

# Классический музыкальный автомат

Thursday, May 21, 2009 4:11 PM by Russian Coding 4 Fun

19 мая 2009 12:09 | Coding4Fun | 0 отзывов Сегодня мы сделаем музыкальный автомат в классическом стиле

# Tune Up Your PC &raquo; Post Topic &raquo; ???????????????????????? ?????????????????????? ??????????????

Pingback from  Tune Up Your PC  &raquo; Post Topic   &raquo; ???????????????????????? ?????????????????????? ??????????????

# My greatest hits…

Thursday, July 02, 2009 1:09 PM by Rudi Grobler

“Your blog is your unedited version of yourself.” - lorelle Here&#39;s some posts that are the most often