Updated : 07-07-10 Added instance specific register methods.

Introduction

Sacha Barber’s Cinch framework provides SimpleCommand, which allows setting ICommand‘s CanExecute() and Execute() method in one place, using lamba statement, it helps makes my code cleaner and thus easier to maintain.

To reduce translation required I used the static RoutedUICommand library provided by WPF, like ApplicationCommands, EditingCommands, as their Text and Shortcut properties are translated already.

I want to use both SimpleCommand and RoutedUICommand, but it’s impossible at the current state, so I inherited a new class from SimpleCommand, which combine two things together, based on Dan Crevier’s CommandModel implementation.

How to use?

SimpleRoutedCommand has a number of constructors, please check the code for more details.

  SimpleRoutedCommand SelectAllCommand =
  new SimpleRoutedCommand(ApplicationCommands.SelectAll)
    {
        CanExecuteDelegate = x => true,
         ExecuteDelegate = x => SelectAll()
    }
  SimpleRoutedCommand.Register(typeof(FileList), SelectAllCommand,
     new KeyGesture(Key.A, ModifierKeys.Control));

And the code

public class SimpleRoutedCommand : SimpleCommand, ICommand
{
    public SimpleRoutedCommand()
    {
        _routedCommand = new RoutedUICommand();
        init();
    }

    public SimpleRoutedCommand(string text)
    {
        RoutedUICommand routedCommand = new RoutedUICommand();
        routedCommand.Text = text;
        _routedCommand = routedCommand;
        init();
    }

    public SimpleRoutedCommand(RoutedUICommand routedCommand)
    {
        _routedCommand = routedCommand;
        init();
    }

    public SimpleRoutedCommand(RoutedUICommand routedCommand, SimpleCommand simpleCommand)
    {
        _routedCommand = routedCommand;
        _embeddedCommand = simpleCommand;
        ExecuteDelegate = x =>
        {
            _embeddedCommand.Execute(x);
            this.CommandSucceeded = simpleCommand.CommandSucceeded;
        };

        CanExecuteDelegate = x =>
        {
            return _embeddedCommand.CanExecute(x);
        };            

        init();
    }

    private void init()
    {
        _commandBinding = new CommandBinding(_routedCommand,
           (ExecutedRoutedEventHandler)delegate(object sender, ExecutedRoutedEventArgs e)
           {
               this.ExecuteDelegate(e.Parameter);
               e.Handled = true;
           },
           (CanExecuteRoutedEventHandler)delegate(object sender, CanExecuteRoutedEventArgs e)
           {
               e.CanExecute = this.CanExecuteDelegate(e.Parameter);
           });
    }

    public override string ToString()
    {
        return _routedCommand.ToString();
    }

    #region Data
    private CommandBinding _commandBinding;
    private RoutedCommand _routedCommand;
    private SimpleCommand _embeddedCommand = null;
    #endregion
    #region Public Properties
    public CommandBinding CommandBinding { get { return _commandBinding; } }
    #endregion

    #region Static Methods
    public static void Register(UIElement element, SimpleRoutedCommand simpleRoutedCommand, InputGesture inputGesture)
    {
        element.InputBindings.Add(new InputBinding(simpleRoutedCommand, inputGesture));
        element.CommandBindings.Add(simpleRoutedCommand.CommandBinding);
    }

    public static void Register(UIElement element, SimpleRoutedCommand simpleRoutedCommand)
    {
        element.CommandBindings.Add(simpleRoutedCommand.CommandBinding);
    }
    //http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!255.entry?wa=wsignin1.0
    public static void Register(Type hostType, SimpleRoutedCommand simpleRoutedCommand, InputGesture inputGesture)
    {
        CommandManager.RegisterClassCommandBinding(hostType, simpleRoutedCommand.CommandBinding);
        CommandManager.RegisterClassInputBinding(hostType, new InputBinding(simpleRoutedCommand, inputGesture));
        simpleRoutedCommand._routedCommand.InputGestures.Add(inputGesture);
    }

    public static void Register(Type hostType, SimpleRoutedCommand simpleRoutedCommand)
    {
        CommandManager.RegisterClassCommandBinding(hostType, simpleRoutedCommand.CommandBinding);
    }
    #endregion
}