Make menus at run time for a WPF application in C#

[Make menus at run time]

This example shows how you can make menus at runtime and add them to a WPF application. Normally you build a WPF program’s user interface at design time using a XAML editor, but anything you can do in XAML you can also do in code if necessary.

The following code builds a menu with some menu items at run time. It gives the menu items accelerators and tooltips, and connects them to event handlers.

private void btnMakeMenus_Click(object sender, RoutedEventArgs e)
    // Make the main menu.
    Menu mainMenu = new Menu();
    mainMenu.HorizontalAlignment =
    mainMenu.VerticalAlignment =

    // Make the File menu.
    MenuItem fileMenuItem = new MenuItem();
    fileMenuItem.Header = "_File";

    // Make the File menu's open item.
    MenuItem openMenuItem = new MenuItem();
    openMenuItem.Header = "_Open";
    openMenuItem.Click += openMenuItem_Click;

    // Give the Open item a tooltip.
    ToolTip openToolTip = new ToolTip();
    openMenuItem.ToolTip = openToolTip;
    openToolTip.Content = "Open a new file";

    // Make the File menu's exit item.
    MenuItem exitMenuItem = new MenuItem();
    exitMenuItem.Header = "E_xit";
    exitMenuItem.Click += exitMenuItem_Click;

    // Give the Exit item a tooltip.
    ToolTip exitToolTip = new ToolTip();
    exitMenuItem.ToolTip = exitToolTip;
    exitToolTip.Content = "End the program";

private void openMenuItem_Click(object sender, RoutedEventArgs e)
    MessageBox.Show("Open a new file here");

private void exitMenuItem_Click(object sender, RoutedEventArgs e)

The code first makes a Menu object and adds it to the main content Grid control’s children. It adds a File menu item to the Menu object’s Items collection.

Next the code creates a menu item to represent the File menu’s Open command and adds it to the File menu item’s Items collection. It adds the openMenuItem_Click method as an event handler for the new item’s Click event. The code then creates a ToolTip and sets the Open menu item’s ToolTip property to it.

The code repeats those steps to make the File menu’s Exit menu item. The rest of the code shows the event handlers used by the Open and Exit MenuItems.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in controls, menus, wpf and tagged , , , , , , , , , , , . Bookmark the permalink.

4 Responses to Make menus at run time for a WPF application in C#

  1. JOHN says:

    Congratulations for this very good blog!

    I have modified your program to make it more useful by adding to it a FileDialog (OpenFileDialog) in the code behind that is very missing in WPF, for viewing large images.

    I don’t understand why there is nothing done in WPF for working with files,directory and common dialog box,
    which were so useful in C#.

  2. Fred says:

    Why do you use the silly constructs:
    “_File” “_Open” “E_xit”
    a need for self-importance and or merit?

    • RodStephens says:

      One could ask the same thing about your comment.

      The underscore character tells the menu system which character is a menu item’s accelerator key. For example, a caption of “_File” makes the menu appear as File and makes it open when you press Alt+F.

    • Brian says:

      hahaha Fred makes an epic faux pas and has to delete his account in shame

Comments are closed.