Windows Scripting
  Home arrow Windows Scripting arrow A Brief Look at Menus in WPF
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

A Brief Look at Menus in WPF
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-09-22

    Table of Contents:
  • A Brief Look at Menus in WPF
  • More on MenuItem
  • Handling Input
  • Context Menus

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Brief Look at Menus in WPF


    (Page 1 of 4 )

    Menus are one of the most important parts of any application. This article will explain why, and show you how to build two kinds of menus using Windows Presentation Foundation. Let's get started.

    An application needs to have functionality in order to be of any use. However, that functionality needs to be accessible to the user. If it isn't, then it means nothing. This requires that the application be organized well. That is, the user must be able to easily figure out how to perform a particular task, and common tasks need to be in very obvious places.

    One of the most important tools in the effort to create an organized and accessible user interface is the menu. Many applications feature a menu at the very top of the window, allowing the user to perform various functions, but many individual controls also have their own menus, called context menus, that enable a user to get increased functionality out of that control.

    In this article, we'll take a look at both regular menus and context menus in Windows Presentation Foundation.

    The Menu and MenuItem classes

    A menu in WPF is represented by the Menu class. Like any other control, an instance of Menu may be created either declaratively or programatically. Here's the basic XAML required to begin creating it declaratively:


    <Menu>

    </Menu>


    Also like any other control, a Menu must be positioned in the application. Of course, the obvious place to put a menu would be at the top of the application. You can get it there using a variety of layout controls-whatever fits your application.

    The Menu class contains a boolean property called IsMainMenu. This property determines whether or not the menu will respond to the Alt or F10 keys, as the main menu of an application would be expected to do. The default value for this property is true. If you only have one menu in your application, then you need not worry about this property, but if you have multiple menus, you'll want to set the IsMainMenu properties of the non-main menus accordingly:


    <Menu IsMainMenu="False">

    </Menu>


    The most important pieces of a menu, though, are its submenus and its items, so that's where we'll need to focus. A Menu is actually an ItemsControl (it inherits from MenuBase, which inherits from ItemsControl), so its items are stored in Items and ItemsSource. A Menu's items take the form of instances of the MenuItem class. A MenuItem can actually be used for multiple purposes. The simplest form it can take is a regular menu item. Normally, the item is given a Header (the text on the item). Here, we create two menu items to start with, File and Edit:


    <Menu>

     <MenuItem Header="File">

     

     </MenuItem>

     <MenuItem Header="Edit">

     

     </MenuItem>

    </Menu>


    You can roll your mouse over File and Edit, and they'll act like normal menu items. But, as you know, File and Edit normally don't behave like this. Rather, they are actually submenus with their own child items. This brings us to the other form of a MenuItem: given children, it acts as a submenu. Within it can be placed other MenuItem objects. Let's add a few items to File and Edit:


    <Menu>

     <MenuItem Header="File">

     <MenuItem Header="New" />

     <MenuItem Header="Open" />

     <MenuItem Header="Save" />

     <MenuItem Header="Quit" />

     </MenuItem>

     <MenuItem Header="Edit">

     <MenuItem Header="Copy" />

     <MenuItem Header="Cut" />

     <MenuItem Header="Paste" />

     </MenuItem>

    </Menu>


    Now, the File and Edit items will act as submenus, with various items under them, and the menu will look more like a normal menu. Of course, another tool to help organize menu items is separators. Separators are very easy to add. We could add one between the Save and Quit items of the above sample menu:


    <MenuItem Header="Save" />

    <Separator />

    <MenuItem Header="Quit" />

    More Windows Scripting Articles
    More By Peyton McCullough


       · Hey, all,Menus are obviously a huge part of many applications. In this article,...
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT