0
votes

I am using a custom RoutedCommand in a context menu. The command is definedlike this:

public static readonly RoutedCommand RoutedAddMainHeading = new RoutedCommand();

And it is accessed like this:

<ContextMenu>
    <MenuItem Header="Add Main Heading" Command="local:JurisdictionVM.RoutedAddMainHeading" />
</ContextMenu>

It works fine, except when my program starts, the command is disabled. After I click on a text box in the window it becomes enabled.

If I use a custom ICommand implementation (with CanExecute always returning true) this doesn't happen, but the command will not be routed.

I tried to call CommandManager.InvalidateRequerySuggested but this doesn't seem to do anything.

I am using a command sink to define the command binding and link it to my ViewModel, which may be causing the problem. I am using an implementation I found here: http://www.codeproject.com/KB/WPF/VMCommanding.aspx

UPDATE: It doesn't look like the custom command binding is the problem. I tried a standard CommandBinding linked to code-behind methods and have the same issue. THe CommandBinding is defined like:

<CommandBinding Command="local:JurisdictionVM.RoutedAddMainHeading" CanExecute="Foo_CanExecute" Executed="Foo_Executed"/>

It seems that this is a problem unique to ManuItem. I do not have this issue if I use this command in a button.

Is there another way to force a re-evaluation of CanExecute?

Or a way to get RoutedCommand to default to enabled (or a way to write a custom one)?

2

2 Answers

0
votes

It looks like this is the same problem as here:

Why is this WPF RoutedCommand bound Context MenuItem disabled?

The solution is to set the focus in the window constructor.