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.
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.
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.
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.
//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);
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With