-1

I've bound a command to a button on a Ribbon control. The CanExecute method on the button gets called as expected but clicking on the button doesn't cause the Execute method to be called. The CanExecute sets the CanExecute property to true - the button in question is enabled and clickable.

Has anyone else seen this behaviour before? If so how do I fix it!

EDIT:

CommandBinding commandBinding = new CommandBinding(StaticCommands.ThisCommand, ThisCommandExecutedHandler, ThisCommandCanExecuteHandler);
CommandManager.RegisterClassCommandBinding(this.GetType(), commandBinding);
CommandBindingList.Add(commandBinding);

StaticCommands.ThisCommand is a RoutedCommand with an input gesture of F5.

Unfortunately I can't post any xaml because everything is wrapped up in another team's libraries. I am assuming that is correct for now. Also, using the keyboard gesture associated with the command (pressing F5) causes the execute method to be called.

There are no exceptions thrown, no messages in the output window, and snoop shows everything bound correctly. I'm really stumped.

Phil Gan
  • 2,813
  • 2
  • 29
  • 38
  • 1
    If `CanExecute()` returns false, it **can't** `Execute()`. `CanExecute()` is called, but what does it return when the button is clicked? – BoltClock Oct 10 '11 at 16:22
  • 1
    If it's a command binding, clicking wouldn't be available if the CanExecute(object) method returned false. – Erik Dietrich Oct 10 '11 at 16:26
  • 1
    @BoltClock could be right. However, if CanExecute() returns true, we would need to see a bit of code to help diagnose it. – mbmcavoy Oct 10 '11 at 16:28
  • 4
    Please post your XAML and the code that defines the command that is being data-bound in your XAML. – Philipp Schmid Oct 10 '11 at 16:37
  • The `CanExecute` returns true. The button is enabled and clickable. – Phil Gan Oct 10 '11 at 16:47
  • To be clear, there is no reason to believe that the command can't `Execute()` if `CanExecute()` returns `false`. `ICommand` is an interface; any implementation can simply ignore the `CanExecute()` method in the `Execute()` implementation and/or always return `true` from its `CanExecute()` implementation. – Jay Oct 11 '11 at 05:54

2 Answers2

2

This usually happens if the parameters dont match in type correctly... are you binding CommandParameter of one type and accepting a different type parameter in the Command.Execute() call?

WPF-it
  • 19,625
  • 8
  • 55
  • 71
  • I'm using the `ExecuteRoutedEventHandler` which just accepts the sender and the event args. Is there a reason that the keyboard gesture would work but not clicking the button? – Phil Gan Oct 11 '11 at 08:41
  • Strange! Does the keyboard gesture refers the same `ICommand`? If yes then this indicates that there could be an issue with `CommandBinding` (as `KeyBinding` for same command works). Call `CommandManager.InvalidateRequerySuggested();` in `Window_loaded` event and see. – WPF-it Oct 11 '11 at 10:40
0

Fixed this by wrapping the RoutedCommands in a RelayCommand. I have no idea why that worked, but assuming there's a problem in the other team's assembly.

Phil Gan
  • 2,813
  • 2
  • 29
  • 38