CommandBinding not set CanExecute will disable button.

Flithor 196 Reputation points

If I create the CommandBinding with no CanExecute:

CommandManager.RegisterClassCommandBinding(typeof(MyClass), new CommandBinding(MyCommand, MyCommandExecuted));

And set the command to a button:

<Button Content="Click Me!" Command="{x:Static local:MyClass.MyCommand}"/>

Than the button will always disabled.

What's wrong with it?

Must I set the CanExecute to enabled button?

Oh well.. that's my mistake.
Look at my answer.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,700 questions
0 comments No comments
{count} votes

Accepted answer
  1. Flithor 196 Reputation points

    Well, the way I use RegisterClassCommandBinding is incorrect...

    The right way to use it is:

    CommandManager.RegisterClassCommandBinding(typeof(Button), new CommandBinding(MyCommand, MyCommandExecuted));

    The first argument of RegisterClassCommandBinding is the target command source type, not the current class type...

    If want to use this command in places other than buttons, use typeof(FrameworkElement).

    But typeof(ICommandSource) is not work.

    0 comments No comments

0 additional answers

Sort by: Most helpful