Question

Whenever I add a Command to a <MenuItem> the MenuItem and foreground is grayed out. If I remove the Command it is styled as normal.

It seems as if the MenuItem gets disabled when it uses a command.

Could this just be a styling issue? I'm using MahApps.Metro which sets the style of my app.

<MenuItem Header="_SETTINGS" Margin="0,0,10,0">
                <MenuItem Header="_View Settings" Command="ApplicationCommands.Properties">
                    <MenuItem.Icon>
                        <Image Height="16" Width="16" Source="../Images/settings-26.png"></Image>
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
Was it helpful?

Solution

You need to attach some CommandBinding for that command otherwise it will be disabled

RoutedCommand.CanExecute

The actual logic that determines if a RoutedCommand can execute on the current command target is not contained in the CanExecute methods, rather CanExecute raises the PreviewCanExecute and the CanExecute events which tunnel and bubble through element tree looking for a object with a CommandBinding. If a CommandBinding for that RoutedCommand is found, then the CanExecuteRoutedEventHandler attached to CommandBinding is called. These handlers supply the programming logic for determining if the RoutedCommand can execute or not.

Without CommandBinding CommandManager.CanExecute event will always came back as false and your button will be disabled

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top