question

Flithor-2893 avatar image
0 Votes"
Flithor-2893 asked Flithor-2893 answered

CommandBinding not set CanExecute will disable button.

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-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Flithor-2893 avatar image
0 Votes"
Flithor-2893 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.