Adding a standard menu to the WPF application
One of the most common parts of WPF applications is the menu, as it gives various options within a very little space. WPF comes with a control named Menu
, to hold items named MenuItem
.
Let's learn more about this menu control and how to add it to Windows applications.
Getting ready
Open your Visual Studio, and create a new WPF project called CH02.MenuDemo
.
How to do it...
Follow these steps to add menus to your WPF application:
- Open the
MainWindow.xaml
page, and replace the defaultGrid
with aDockPanel
. We will discuss more about this panel in the next chapter. - Now add the
Menu
control inside theDockPanel
. This will create the base to hold all the menu items. - You can then add root-level menu items and sub-menu items in a hierarchical fashion, as shown in the following code snippet:
<DockPanel> <Menu> <MenuItem Header="File"> <MenuItem Header="New" /> <MenuItem Header="Open" /> ...