Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: How can you add a new menuitem to a menu at runtime?

Tags:

I have a simple WPF application with a menu. I need to add menu items dynamically at runtime. When I simply create a new menu item, and add it onto its parent MenuItem, it does not display in the menu, regardless of if UpdateLayout is called.

What must happen to allow a menu to have additional items dynamically added at runtime?

Note: the following code doesn't work.

            MenuItem mi = new MenuItem();             mi.Header = "Item to add";             mi.Visibility = Visibility.Visible;             //addTest is a menuitem that exists in the forms defined menu             addTest.Items.Add(mi);             addTest.UpdateLayout(); 

At the moment, the default menu items are defined in the xaml file. I want to add additional menu items onto this menu and its existing menu items. However, as stated, the above code does nothing.

like image 513
Jon Ediger Avatar asked Nov 14 '08 21:11

Jon Ediger


People also ask

What .NET WPF class we use to create menus which namespace it belongs to?

WPF Menu control is represented by the Menu class in C#. This menus tutorial and menus code examples explain how to use menus in WPF using C#. In WPF, the Menu and the MenuItem classes represent a menu and a menu item respectively. A Menu is a collection of menu items with a command associated with each menu item.

Which XAML tag is used for menu bar?

The <Menu> and <MenuItem> XAML elements are used to create menus in XAML. Learn how to use menu and menu item in a WPF app.

What are controls in WPF?

WPF SDK continues to use the term "control" to loosely mean any class that represents a visible object in an application, it is important to note that a class does not need to inherit from the Control class to have a visible presence.


2 Answers

//Add to main menu MenuItem newMenuItem1 = new MenuItem(); newMenuItem1.Header = "Test 123"; this.MainMenu.Items.Add(newMenuItem1);  //Add to a sub item MenuItem newMenuItem2 = new MenuItem(); MenuItem newExistMenuItem = (MenuItem)this.MainMenu.Items[0]; newMenuItem2.Header = "Test 456"; newExistMenuItem.Items.Add(newMenuItem2); 
like image 85
Whytespot Avatar answered Sep 20 '22 19:09

Whytespot


I have successfully added menu items to a pre-defined menu item. In the following code, the LanguageMenu is defined in design view in th xaml, and then added the sub items in C#.

XAML:

<MenuItem Name="LanguageMenu" Header="_Language">   <MenuItem Header="English" IsCheckable="True" Click="File_Language_Click"/> </MenuItem> 

C#:

// Clear the existing item(s) (this will actually remove the "English" element defined in XAML) LanguageMenu.Items.Clear();   // Dynamically get flag images from a specified folder to use for definingthe menu items  string[] files = Directory.GetFiles(Settings.LanguagePath, "*.png"); foreach (string imagePath in files) {   // Create the new menu item   MenuItem item = new MenuItem();    // Set the text of the menu item to the name of the file (removing the path and extention)   item.Header = imagePath.Replace(Settings.LanguagePath, "").Replace(".png", "").Trim("\\".ToCharArray());   if (File.Exists(imagePath))   {     // Create image element to set as icon on the menu element     Image icon = new Image();     BitmapImage bmImage = new BitmapImage();     bmImage.BeginInit();     bmImage.UriSource = new Uri(imagePath, UriKind.Absolute);     bmImage.EndInit();     icon.Source = bmImage;     icon.MaxWidth = 25;     item.Icon = icon;   }    // Hook up the event handler (in this case the method File_Language_Click handles all these menu items)   item.Click += new RoutedEventHandler(File_Language_Click);     // Add menu item as child to pre-defined menu item   LanguageMenu.Items.Add(item); // Add menu item as child to pre-defined menu item } 
like image 27
awe Avatar answered Sep 22 '22 19:09

awe