views:

558

answers:

1

Hi,

When I disable a control in WPF, like say a Menu item, like

MenuItem aMenuItem = ... aMenuItem.IsEnabled = false;

the text in the MenuItem is still active, that is it is not grayed out as you would expect when items are disabled.

Is there a simple way to do this not only for Menu items but for any WPF control?

Thanks

+1  A: 

yes by using commands. MenuItems and Buttons have a command property. a Command is an implementation of the ICommand interface which has a method called CanExecute. When can execute is called if it returns true the menutitem or button is enabled, otherwise it is greyed out.

MSDN Command overview

nice simple tutorial on setting up commands

google search :)

Aran Mulholland
I setup the menuitems using commands, it still does not gray out when in the CanExecute method is set to false.Thanks
Allen Ho
have you got your own style on the menu items? if you have you may be overriding the default greyed out implementation. when you click on the menu item that should be greyed out does the execute method of the command fire?
Aran Mulholland
Thanks that was pretty much it, I have a Global style that tagrets TextBlocks, so I can change the colour of all TextBlocks in the application. It seems MenuItems have TextBlocks in their visual tree.Not sure how to get around this so I have the best of both worlds where I can change all text colors and make sure they are grayed out when they are disabled.I assume I have to write a control template that targets MenuItems for this then.Thanks
Allen Ho
mark it as answered then
Aran Mulholland