.NET
  Home arrow .NET arrow Page 2 - Commands, Input and the 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? 
.NET

Commands, Input and the WPF
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-07-17

    Table of Contents:
  • Commands, Input and the WPF
  • Defining commands
  • Input Bindings
  • Command Source

  • 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


    Commands, Input and the WPF - Defining commands


    (Page 2 of 4 )

    Example 4-17 shows how to define a custom command. WPF uses object instances to establish the identity of commands—if you were to create a second command of the same name, it would not be treated as the same command. Because commands are identified by their command objects rather than their names, commands are usually put in public static fields or properties.

    Example 4-17. Creating a custom command

    ...
    using System.Windows.Input;

    namespace MyNamespace {

        public class MyAppCommands {
       
    public static RoutedUICommand AddToBasketCommand;

        static MyAppCommands() {
          InputGestureCollection addToBasketInputs =
                new InputGestureCollection();
          addToBasketInputs.Add(new KeyGesture(
              Key.B, ModifierKeys.Control|ModifierKeys.Shift));
          AddToBasketCommand = new RoutedUICommand(
             
    "Add to Basket", "AddToBasket",
             
    typeof(MyAppCommands), addToBasketInputs);
         
    }
       }
    }

    The first RoutedUICommand constructor parameter is the name as it should appear in the user interface. In a localizable application, you would use a mechanism such as the .NET class library’s ResourceManager to retrieve a localized string rather than hardcoding it. The second constructor parameter is the internal name of the command as used from code—this should match the name of the field in which the command is stored, with the command suffix removed.

    As with the built-in commands, your application command doesn’t do anything on its own. It’s just an identifier. You will need to supply command bindings to implement the functionality. You will also typically want to associate the command with menu items or buttons.

    Using commands in XAML

    Example 4-18 shows a Button associated with the standard Copy command.

    Example 4-18. Invoking a command with a Button

    <Button Command="Copy">Copy</Button>

    Because this example uses a standard command from theApplicationCommandsclass, we can use this short form syntax, specifying nothing but the command name.

    However, for commands not defined by the classes in Table 4-4, a little more information is required. The full syntax for a command attribute in XAML is:

      [[xmlNamespacePrefix:]ClassName.] EventName

    If only the event name is present, the event is presumed to be one of the standard ones. For example,Undois shorthand forApplicationCommands.Undo. Otherwise, you must also supply a class name and possibly a namespace prefix. The namespace prefix is required if you are using either custom commands, or commands defined by some third-party component. This is used in conjunction with a suitable XML namespace declaration to make external types available in a XAML file. (See Appendix A for more information on clr-namespaceXML namespaces.)

    Example 4-19 shows the use of the command-name syntax with all the parts present. The value ofm:MyAppCommands.AddToBasketCommandmeans that the command in question is defined in theMyNamespace.MyAppCommandsclass in theMyLib component, and is stored in a field calledAddToBasketCommand.

    Example 4-19. Using a custom command in XAML

    <Window xmlns:m="clr-namespace:MyNamespace;assembly=MyLib" ...>
       ...
      
    <Button Command="m:MyAppCommands.AddToBasketCommand"> Add to Basket</Button>
       ...

    Because commands represent the actions performed at the user’s request, it’s likely that some commands will be invoked very frequently. It is helpful to provide keyboard shortcuts for these commands in order to streamline your application for expert users. For this, we turn to input bindings.

    More .NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming WPF, Second Edition,"...
     

    Buy this book now. This article is excerpted from Programming WPF, Second Edition, written by Chris Sells and Ian Griffiths (O'Reilly, 2007; ISBN: 0596510373). Check it out today at your favorite bookstore. Buy this book now.

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





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