Avoiding CommandBinding in the XAML Code Behind files
Yesterday I got a very good question from a colleague. Basically we were discussing the M-V-VM pattern and how it makes your XAML code behind much cleaner + your application more testable. The only thing that was annoying him was the fact that he had to do the command binding and delegate the work to the ViewModel . So he asked me, “Is there a way how I can remove the Command Binding from my code behind as well?”. What he was referring to, was the hooking of Commands to the ViewModel, which would look something like this
1: CommandBindings.Add(
2: new CommandBinding(Commands.Search,
3: (x, e) => vm.Search(),
4: (x, e) => e.CanExecute = vm.CanSearch())
5: );
And the answer is, of course you can! After all, is there something you cannot do with the mighty WPF
So how can we achieve this? We have to create a class that implements the ICommand interface. Here is how the ICommand interface looks like.
1: public interface ICommand
2: {
3: event EventHandler CanExecuteChanged;
4:
5: bool CanExecute(object parameter);
6:
7: void Execute(object parameter);
8: }
Very simple isn’t it! Here is the MSDN description of the methods of this interface

CanExecute
Defines the method that determines whether the command can execute in its current state.

Execute
Defines the method to be called when the command is invoked.

CanExecuteChanged
Occurs when changes occur that affect whether or not the command should execute.
So in the CanExecute we have to put the logic that checks if we can execute the command and in the Execute we put the logic that executes the command…. but what about the CanExecuteChanged event? Well here we have to hook up to the Command System. Here is a simple implementation of the ICommand.
1: public class SearchCommand : ICommand
2: {
3: ViewModel viewModel;
4: public SearchCommand(ViewModel viewModel)
5: {
6: this.viewModel = viewModel;
7: }
8:
9: #region ICommand Members
10:
11: public bool CanExecute(object parameter)
12: {
13: return !String.IsNullOrEmpty(viewModel.SearchText);
14: }
15:
16: public event EventHandler CanExecuteChanged
17: {
18: add { CommandManager.RequerySuggested += value; }
19: remove { CommandManager.RequerySuggested -= value; }
20: }
21:
22: public void Execute(object parameter)
23: {
24: viewModel.MyDataView.Filter = x => ((Person)x).Name.Contains(viewModel.SearchText);
25: }
26:
27: #endregion
28: }
As you can see in order to hook up to the WPF command system we have to override the default behavior of the event and register to the CommandManager.RequerySuggested like so
1: public event EventHandler CanExecuteChanged
2: {
3: add { CommandManager.RequerySuggested += value; }
4: remove { CommandManager.RequerySuggested -= value; }
5: }
This is very important because by doing so the CanExecute method will be called when ever there is focus changed, user input etc…
Once you have a class that implements the ICommand you can expose that class in your ViewModel and databind to it in the Command property of the control you want to hook up the command to like so
1: ViewModel property
2: /// <summary>
3: /// Return an ICommand that can execute the search
4: /// </summary>
5: public ICommand SearchByNameCommand
6: {
7: get { return searchByNameCommand; }
8: }
9:
10: XAML to hook to that command
11: <Button Command="{Binding SearchByNameCommand}" Content="Search" />
And that’s it… No more command bindings to hook to the Command in the code behind of the XAML file! This is very cool but now I guess the next question would be, so we actually have to create a class and implement an ICommand every time??
Well the answer is HELL NO…. What I do is create a class that does all the stuff for me and then I pass a delegate for the CanExecute and another one for the Execute… as simple as this…
1: public class SimpleCommand : ICommand
2: {
3: public Predicate<object> CanExecuteDelegate { get; set; }
4: public Action<object> ExecuteDelegate { get; set; }
5:
6: #region ICommand Members
7:
8: public bool CanExecute(object parameter)
9: {
10: if (CanExecuteDelegate != null)
11: return CanExecuteDelegate(parameter);
12: return true;// if there is no can execute default to true
13: }
14:
15: public event EventHandler CanExecuteChanged
16: {
17: add { CommandManager.RequerySuggested += value; }
18: remove { CommandManager.RequerySuggested -= value; }
19: }
20:
21: public void Execute(object parameter)
22: {
23: if (ExecuteDelegate != null)
24: ExecuteDelegate(parameter);
25: }
26:
27: #endregion
28: }
and to create a command now is as simple as this…
1: searchByNameCommand = new SimpleCommand
2: {
3: CanExecuteDelegate = x => !String.IsNullOrEmpty(SearchText),
4: ExecuteDelegate = x => myDataView.Filter = stateObj => ((Person)stateObj).Name.Contains(SearchText)
5: };
and yes you could say that it is a 1 line of code
(well a long one maybe
)… yet of course, you can have as much logic in there as you want!
If you want to see a sample project I created a demo app that you can download here.





Like it very much young marlon
This is a nice article Marlon. I saw a very similar approach on a Model View ViewModel video a couple last week.
My question is, how do you handle input gestures with this technique ? How do you do to associate a shortcut to your command ?
thanks sacha
Jeremy that is really a good question … yet I am not able to answer it today because I never did that… when ever I needed that I relied on the Routed Commands…. a good next post I must say
Much cleaner! Thanks chief
Great, looks sexier now
[...] Marlon Grech on Avoiding CommandBinding in the XAML Code Behind files [...]
Pingback by 2008 November 27 - Links for today « My (almost) Daily Links | November 27, 2008 |
[...] Avoiding CommandBinding in the XAML Code Behind Files (Marlon Grech) [...]
Pingback by Dew Drop - November 28, 2008 | Alvin Ashcraft's Morning Dew | November 28, 2008 |
[...] Marlon Grech discussed Avoiding CommandBinding in the XAML Code Behind files. [...]
Pingback by Visual Studio Links #86 : Visual Studio Hacks | November 29, 2008 |
Jeremy,
You can bind shortcut keys like this
InputBindings.Add(new KeyBinding(viewModel.SearchByNameCommand, Key.F3, ModifierKeys.None));
Regards
[...] trying to separate concerns by using patterns such as M-V-VM. If you haven’t had a look at my previous post on the ICommand I suggest that you do that before reading this [...]
Pingback by AttachedCommandBehavior aka ACB « C# Disciples | December 4, 2008 |
[...] week, Marlon blogged about how to avoid adding command binding code to code-behind files. The idea is both simple and great, and allows you to keep your code behind nice and clean, and [...]
Marlon – That’s a pretty cool idea. What do you think about this approach? http://codingcontext.wordpress.com/2008/12/10/commandbindings-in-mvvm/
Anyone know how to write that CanExecuteChanged EventHandler in Vb.net code?
ie.
public event EventHandler CanExecuteChanged
{add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; }
}
[...] ways though… the organizer is due for a bit of refactoring (I’m planning to clean it up and use Marlon’s advice on command bindings, among other things) so I’m writing some tests to make sure it still works after that, like a [...]
[...] Marlon Grech – SimpleCommand [...]
Pingback by The power of ICommand - Rudi Grobler | March 5, 2009 |
[...] Where SimpleCommand is a class that implements the ICommand interface. For more info on this visit this post. [...]
Pingback by [MVVM + Mediator + ACB = cool WPF App] – The MVVM « C# Disciples | April 13, 2009 |
[...] am actually using Marlon Grechs SimpleCommand but if you prefer you can use Prisms DelegateCommand, or Josh Smiths RelayCommand, they all [...]
Pingback by sachabarber.net » WPF : Attached Commands | May 2, 2009 |
[...] Simple Command (almost identical to Relay Command but worth reading) This entry was posted in Uncategorized. Bookmark the permalink. ← View Models: POCOs versus DependencyObjects [...]
Pingback by WPF ICommand vs RoutedCommand | Grimcoder blog | September 18, 2010 |
That CanExecuteChanged implementation bothers me a bit — CommandManager.RequerySuggested is documented as only holding a weak reference to attached handlers. Which suggests that it wouldn’t work reliably in this case.
Thanks so much for the great post. It removes most of the junk that we have to create a command binding in WPF. But in real application, the anonymous functions which are used as Predicate and Action are not realistic. Most of the time I have to use a function instead
But this is really a nice post. I’ve learned a lot from this