The following code creates a new menu with some items.
Code:
private void button1_Click(object sender, RoutedEventArgs e)
{
// Make the main menu.
Menu mainMenu = new Menu();
contentGrid.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 items.
MenuItem openMenuItem = new MenuItem();
fileMenuItem.Items.Add(openMenuItem);
openMenuItem.Header = "_Open";
openMenuItem.Click += openMenuItem_Click;
ToolTip openToolTip = new ToolTip();
openMenuItem.ToolTip = openToolTip;
openToolTip.Content = "Open a new file";
MenuItem exitMenuItem = new MenuItem();
fileMenuItem.Items.Add(exitMenuItem);
exitMenuItem.Header = "E_xit";
exitMenuItem.Click += exitMenuItem_Click;
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();
}
It's a lot easier to just use XAML.
The following code creates a new top-level grid:
Code:
private void button2_Click(object sender, RoutedEventArgs e)
{
Grid newGrid = new Grid();
this.Content = newGrid;
}
This replaces all of the window's current controls so you'll have to build an interface in code if you want the program to do anything.
If you want to put new controls inside an existing control, it's usually easiest to give the control a name so you can refer to it directly.
I hope that helps.