Menus
Many windows applications provide access to their functionality
through a hierarchy of menus. These are typically presented either as a
main menu at the top of the window, or as a pop-up "context" menu. WPF
provides two menu controls. Menu
is
for permanently visible menus (such as a main menu), and ContextMenu
is for context menus.
Tip
Menus in pre-WPF Windows applications are typically treated
differently from other user interface elements. In Win32, menus get a
distinct handle type and special event handling provisions. In Windows
Forms, most visible elements derive from a Control
base class, but menus do not. This
means that menus tend to be somewhat inflexible—some user interface
toolkits choose not to use the built-in menu handling in Windows
simply to avoid the shortcomings. In WPF, menus are just normal
controls, so they do not have any special features or
restrictions.
Both kinds of menus are built in the same way—their contents
consist of a hierarchy of MenuItem
elements. Example 5-22 shows a typical example.
Example 5-22. A main menu
<Menu> <MenuItem Header="_File"> <MenuItem Header="_New" /> <MenuItem Header="_Open..." /> <MenuItem Header="_Save" /> <MenuItem Header="Sa_ve As..." /> <Separator /> <MenuItem Header="Page Se_tup..." /> <MenuItem Header="_Print..." /> <Separator /> <MenuItem Header="E_xit" /> </MenuItem> <MenuItem Header="_Edit"> <MenuItem Header="_Undo" /> <MenuItem Header="_Redo" /> <Separator /> <MenuItem Header="Cu_t" /> <MenuItem Header="_Copy" /> <MenuItem ...
Get Programming WPF, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.