How to create a Menu at Run-time in WPF

The following code creates a menu and adds menu items dynamically.

Menu mainMenu = new Menu();

mainMenu.Background = Brushes.LightGreen;

mainMenu.Height = 300;

mainMenu.Width = 200;

 

MenuItem item1 = new MenuItem();

item1.Width = 50;

item1.Header = "First";

mainMenu.Items.Add(item1);

 

MenuItem item2 = new MenuItem();

item2.Width = 50;

item2.Header = "Two";

item1.Items.Add(item2);

 

MenuItem item3 = new MenuItem();

item3.Width = 50;

item3.Header = "Third";

item1.Items.Add(item3);