Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 - Handling Input


    (Page 3 of 4 )

    When a user clicks a menu item, something will need to be done in response. The Click event is raised when a menu item is clicked, so this is the obvious place to give the user a response. The Click attribute can be used to point to an appropriate event handler:


    <MenuItem Header="Quit" Click="MenuItem_Click" />


    The actual event handler would look something like this:


    private void MenuItem_Click(object sender, RoutedEventArgs e)

    {

     // Do something

    }


    But recall from earlier that it's possible to make a menu item checkable. If this is the case, then it's a good idea to respond when the user has checked the menu item and when the user has unchecked the menu item. When the user checks an item, a Checked event is raised, and when the user unchecks an item, an Unchecked event is raised:


    <MenuItem Header="Syntax Highlighting" IsCheckable="True"

     Checked="MenuItem_Checked"

     Unchecked="MenuItem_Unchecked" />


    private void MenuItem_Checked(object sender, RoutedEventArgs e)

    {

     // The user has checked the item

    }


    private void MenuItem_Unchecked(object sender, RoutedEventArgs e)

    {

     // The user has unchecked the item

    }


    Working with the above three events allows for a consistent response each time. However, sometimes, the effect of a menu item changes depending on what the user is actually doing. For example, consider the Edit menu. The use can copy, cut and paste text using this menu. However, this is only appropriate with text boxes, and the exact targets may vary. Furthermore, the user can copy, cut and paste in other ways as well, so the menu shouldn't be the only originator of these events.

    This is where commands come into play. Commands allow for various sources to originate a single action, and for the target to handle the action in its own way. A user may generate a Paste command through the main menu or through a keyboard shortcut, but the same command is generated both times. The same command can have different effects, however, depending on the target. One text box may allow formatting in the pasted text, and another text box may strip all formatting away.

    Linking menu options to commands is very easy. The Command property of a MenuItem simply needs to be set to the desired command (represented by an ICommand). For example, say we wanted to link Copy, Cut and Paste events to menu items. These are fairly common commands, and their functionality is already set up for us (it's of course possible to create your own commands, but this article can't possibly cover all of that; we'll stick to the relationship between commands and menus). We'd set it all up like this:


    <MenuItem Header="Copy"

     Command="ApplicationCommands.Copy"/>

    <MenuItem Header="Cut"

     Command="ApplicationCommands.Cut" />

    <MenuItem Header="Paste"

     Command="ApplicationCommands.Paste" />


    Not only does it link the menu item with the command, but it also disables the menu items where appropriate (such as when there's nothing to copy, cut or paste), and it displays the appropriate keyboard shortcuts beside each item. In fact, we can even remove the headers if we're fine with the standard ones-they will automatically be set:


    <MenuItem Command="ApplicationCommands.Copy"/>

    <MenuItem Command="ApplicationCommands.Cut" />

    <MenuItem Command="ApplicationCommands.Paste" />

    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

    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...
    - Modifying XML Files in WSH
    - Reading XML Files in WSH
    - Visual Basic 2005 XML Programming Using XML ...
    - Creating an XML Document in WSH
    - 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

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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