tags:

views:

2574

answers:

2

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.

+2  A: 
//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);
Whytespot
This doesn't work for me, and mirrors what I've tried. What am I missing? Please see the example text that is not working.
Jon Ediger
I just added a new menu and ran the code with the default control properties and it worked as expected. I tried your code, and it worked on my machine. Create a new project, add a basic menu and don't adjust any of it's properties and try your code again.
Whytespot
The key appears to be to not pre-define any menu items. When I did pre-define them, I couldn't dynamically add them. When I didn't pre-define them, I could.
Jon Ediger
A: 

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
}
awe