A Brief Look at Menus in WPF - More on MenuItem
(Page 2 of 4 )
In some situations, more functionality is needed for menu items. In certain situations, some menu items may need to be disabled. For example, consider a context menu for a standard text box, containing Copy, Cut and Paste menu items. If no text is highlighted, then it doesn't make sense for the user to be able to copy or cut anything. So, it makes sense for those options to be disabled.
Whether or not a menu item is enabled is determined by the value of IsEnabled. You can set IsEnabled to false in order to disable a menu item. For example, below, the Copy and Cut menu items would be disabled:
<MenuItem Header="Copy" IsEnabled="False" />
<MenuItem Header="Cut" IsEnabled="False" />
<MenuItem Header="Paste" />
Another useful bit of functionality is a check box of sorts on a menu item. Some menu items can be made to turn a feature on or off. If the feature is on, a check mark will be placed to the left of the menu item's text. If the feature is off, then the check mark will not appear.
This functionality is easy to implement. It involves two properties: IsCheckable and IsChecked. The IsCheckable property determines whether or not the item can be checked, and the IsChecked property determines whether or not the item actually is checked. The default value for IsChecked is false, so creating a checkable menu item without setting IsChecked will result in an unchecked menu item.
Below, we create a menu item that can be checked:
<MenuItem Header="Syntax Highlighting" IsCheckable="True" />
And here, we create a menu item that is checked by default:
<MenuItem Header="Standard Mode" IsCheckable="True"
IsChecked="True" />
In order to make menus more accessible, it's a good idea to provide access keys for menu items. This will not only make keyboard-loving users happy, but it will allow people with various disabilities to use your application. The presence of an access key is indicated by an underlined letter in an item's label. The item can be "clicked" by pressing Alt and the underlined letter.
Access keys are very easy to implement. The process is the same as with any other control. When specifying the MenuItem's Header, simply add an underscore before the desired access key character. For example, with a File menu, an access key might be set up like this:
<MenuItem Header="_File">
...
</MenuItem>
Pressing the Alt key will show the F as being underlined, and then pressing the F key will activate the File menu.
Next: Handling Input >>
More Windows Scripting Articles
More By Peyton McCullough