[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

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

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

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(); grdContent.Children.Add(mainMenu); mainMenu.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch; mainMenu.VerticalAlignment = System.Windows.VerticalAlignment.Top; // Make the File menu. MenuItem fileMenuItem = new MenuItem(); fileMenuItem.Header = "_File"; mainMenu.Items.Add(fileMenuItem); // Make the File menu's open item. MenuItem openMenuItem = new MenuItem(); fileMenuItem.Items.Add(openMenuItem); 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(); fileMenuItem.Items.Add(exitMenuItem); 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) { MessageBox.Show("Goodbye!"); this.Close(); }

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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.