Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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#

Hello,

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.
 
Old November 1st, 2010, 11:54 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
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.

Rod,

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.

Chris.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.