Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4
This is the forum to discuss the Wrox book WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4 by ; ISBN: 9780470477229
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 1st, 2010, 12:01 AM
Registered User
Join Date: Oct 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating a Menu Programmatically in WPF/C#


I am new to WPF and I am trying to display a menu programmatically (no XAML). How do I do this? All the examples seem to refer to XAML.

Similarly, how would I create a Grid and place it in my top window? Your help is greatly appreciated.

Thanks. Chris.
  #2 (permalink)  
Old November 1st, 2010, 11:54 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts

The following code creates a new menu with some items.

private void button1_Click(object sender, RoutedEventArgs e)
    // Make the main menu.
    Menu mainMenu = new Menu();
    mainMenu.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
    mainMenu.VerticalAlignment = System.Windows.VerticalAlignment.Top;

    // Make the File menu.
    MenuItem fileMenuItem = new MenuItem();
    fileMenuItem.Header = "_File";

    // Make the File menu's items.
    MenuItem openMenuItem = new MenuItem();
    openMenuItem.Header = "_Open";
    openMenuItem.Click += openMenuItem_Click;
    ToolTip openToolTip = new ToolTip();
    openMenuItem.ToolTip = openToolTip;
    openToolTip.Content = "Open a new file";

    MenuItem exitMenuItem = new MenuItem();
    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)
It's a lot easier to just use XAML.

The following code creates a new top-level grid:

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.

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #3 (permalink)  
Old November 1st, 2010, 10:06 PM
Registered User
Join Date: Oct 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Thanks - That works.


Thanks a lot for your help - that works!

I missed the linkage between the window content and the grid (my only help right are books and the internet) within which the menu is placed (I was too focused on the old Windows model).

You are right, it's more work. But in my case, I am parsing messages already defined form the business layer. Therefore, when my library of high level GUI elements is created, I am done.


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating code programmatically mumphis Excel VBA 0 September 2nd, 2005 09:29 AM
Report - programmatically creating penta Access 12 May 19th, 2005 04:53 PM
Creating a table programmatically Bob Burns ASP.NET 1.0 and 1.1 Basics 2 July 15th, 2004 11:44 AM
Creating report programmatically liliarum BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 July 6th, 2004 09:34 AM
Creating Tables Programmatically stu9820 ASP.NET 1.0 and 1.1 Basics 10 October 22nd, 2003 04:25 PM

All times are GMT -4. The time now is 07:23 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.