A Brief Look at Menus in WPF
(Page 1 of 4 )
Menus are one of the most important parts of any application. This article will explain why, and show you how to build two kinds of menus using Windows Presentation Foundation. Let's get started.
An application needs to have functionality in order to be of any use. However, that functionality needs to be accessible to the user. If it isn't, then it means nothing. This requires that the application be organized well. That is, the user must be able to easily figure out how to perform a particular task, and common tasks need to be in very obvious places.
One of the most important tools in the effort to create an organized and accessible user interface is the menu. Many applications feature a menu at the very top of the window, allowing the user to perform various functions, but many individual controls also have their own menus, called context menus, that enable a user to get increased functionality out of that control.
In this article, we'll take a look at both regular menus and context menus in Windows Presentation Foundation.
The Menu and MenuItem classes
A menu in WPF is represented by the Menu class. Like any other control, an instance of Menu may be created either declaratively or programatically. Here's the basic XAML required to begin creating it declaratively:
<Menu>
</Menu>
Also like any other control, a Menu must be positioned in the application. Of course, the obvious place to put a menu would be at the top of the application. You can get it there using a variety of layout controls-whatever fits your application.
The Menu class contains a boolean property called IsMainMenu. This property determines whether or not the menu will respond to the Alt or F10 keys, as the main menu of an application would be expected to do. The default value for this property is true. If you only have one menu in your application, then you need not worry about this property, but if you have multiple menus, you'll want to set the IsMainMenu properties of the non-main menus accordingly:
<Menu IsMainMenu="False">
</Menu>
The most important pieces of a menu, though, are its submenus and its items, so that's where we'll need to focus. A Menu is actually an ItemsControl (it inherits from MenuBase, which inherits from ItemsControl), so its items are stored in Items and ItemsSource. A Menu's items take the form of instances of the MenuItem class. A MenuItem can actually be used for multiple purposes. The simplest form it can take is a regular menu item. Normally, the item is given a Header (the text on the item). Here, we create two menu items to start with, File and Edit:
<Menu>
<MenuItem Header="File">
</MenuItem>
<MenuItem Header="Edit">
</MenuItem>
</Menu>
You can roll your mouse over File and Edit, and they'll act like normal menu items. But, as you know, File and Edit normally don't behave like this. Rather, they are actually submenus with their own child items. This brings us to the other form of a MenuItem: given children, it acts as a submenu. Within it can be placed other MenuItem objects. Let's add a few items to File and Edit:
<Menu>
<MenuItem Header="File">
<MenuItem Header="New" />
<MenuItem Header="Open" />
<MenuItem Header="Save" />
<MenuItem Header="Quit" />
</MenuItem>
<MenuItem Header="Edit">
<MenuItem Header="Copy" />
<MenuItem Header="Cut" />
<MenuItem Header="Paste" />
</MenuItem>
</Menu>
Now, the File and Edit items will act as submenus, with various items under them, and the menu will look more like a normal menu. Of course, another tool to help organize menu items is separators. Separators are very easy to add. We could add one between the Save and Quit items of the above sample menu:
<MenuItem Header="Save" />
<Separator />
<MenuItem Header="Quit" />
Next: More on MenuItem >>
More Windows Scripting Articles
More By Peyton McCullough