.NET
  Home arrow .NET arrow Page 4 - Keyboard and Ink Input with 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

Keyboard and Ink Input with WPF
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-07-10

    Table of Contents:
  • Keyboard and Ink Input with WPF
  • Keyboard State
  • Ink Input
  • Commands

  • 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


    Keyboard and Ink Input with WPF - Commands


    (Page 4 of 4 )

     

    The input events we’ve examined give us a detailed view of user input directed at individual elements. However, it is often helpful to focus on what the user wants our application to do, rather than how she asked us to do it. WPF supports this through the command abstraction—a command is an action the application performs at the user’s request.

    The way in which a command is invoked isn’t usually important. Whether the user presses Ctrl-C, selects the Edit -> Copy menu item, or clicks the Copy button on the toolbar, the application’s response should be the same in each case: it should copy the current selection to the clipboard. The event system we examined earlier in this chapter regards these three types of input as being unrelated, but WPF’s command system lets you treat them as different expressions of the same command.

    The command system lets a UI element provide a single handler for a command, reducing clutter and improving the clarity of your code. It enables a more declarative style for UI elements; by associating aMenuItemorButtonwith a particular command, you are making a clearer statement of the intended behavior than you would by wiring upClick event handlers. Example 4-15 illustrates how commands can simplify things.

    Example 4-15. Commands with a menu and text box

    <DockPanel>
      <Menu DockPanel.Dock="Top">
        <MenuItem Header="_Edit">
         
    <MenuItem Header="Cu_t" Command="ApplicationCommands.Cut" />
          <MenuItem Header="_Copy" Command="ApplicationCommands.Copy" />
          <MenuItem Header="_Paste" Command="ApplicationCommands.Paste" />

        </MenuItem>
      </Menu>
      <ToolBarTray DockPanel.Dock="Top">
        <ToolBar>
         
    <Button Command="Cut" Content="Cut" />
         
    <Button Command="Copy" Content="Copy" />
         
    <Button Command="Paste" Content="Paste" />
        </ToolBar>
      </ToolBarTray>

      <TextBox />
    </DockPanel>

    Each menu item is associated with a command. This is all that’s required to invoke these clipboard operations on the text box; we don’t need any code or event handlers because theTextBoxclass has built-in handling for these commands. More subtly, keyboard shortcuts also work in this example: the built-in cut, copy, and paste commands are automatically associated with their standard keyboard shortcuts, so these work wherever you use a text box. WPF’s command system ensures that when commands are invoked, they are delivered to the appropriate target, which in this case is the text box.

    You are not obliged to use commands. You may already have classes to represent this idea in your own frameworks, and if WPF’s command abstraction does not suit your needs, you can just handle the routed events offered by menu items, buttons, and toolbars instead. But for most applications, commands simplify the way your application deals with user input.

    There are five concepts at the heart of the command system:

    Command object
      
    An object identifying a particular command, such as
       copy or paste

    Input binding
      
    An association between a particular input (e.g., Ctrl-
       C) and a command (e.g., Copy)

    Command source
      
    The object that invoked the command, such as a
       Button, or an input binding

    Command target
      
    The UI element that will be asked to execute the
       command—typically the control that had the keyboard
       focus when the command was invoked

    Command binding
      
    A declaration that a particular UI element knows how
       to handle a particular command

    Not all of these features are explicitly visible in Example 4-15—the command bindings are buried inside the text box’s implementation, and although input bindings are in use (Ctrl-C will work just fine, for example), they’ve been set up implicitly by WPF. To make it a bit easier to see all of the pieces, let’s look at a slightly more complex example that uses all five concepts explicitly (see Example 4-16).

    Example 4-16. Basic command handling

    <!-- XAML -->
    <Window ...>
      <Grid>
        <Button Command="ApplicationCommands.Properties"
                      
    Content="_Properties"/>
      </Grid>
    </Window>
    // Codebehind
    public partial class Window1 : Window {

        public Window1(){
            InitializeComponent();

           InputBinding ib = new InputBinding(
            ApplicationCommands.Properties,
            new KeyGesture(Key.Enter, ModifierKeys.Alt)); 
           this.InputBindings.Add(ib);

           CommandBinding cb = new CommandBinding(ApplicationCommands.Properties); 
           cb.Executed += new ExecutedRoutedEventHandler(cb_Executed);
           this.CommandBindings.Add(cb);
       
    }

        void cb_Executed(object sender, ExecutedRoutedEventArgs e) {
            MessageBox.Show("Properties");
        }
     

    }

    This example uses the standardApplicationCommands.Propertiescommand object. Applications that support this command would typically open a property panel or window for the selected item. The XAML in this example associates a button with this command object; clicking the button will invoke the command. The code behind establishes an input binding so that the Alt-Enter shortcut may also be used to invoke the command. Our example, therefore, has two potential command sources: the button and the input binding. The command target in this particular example will be the button; this is true even if the command is invoked with a keyboard shortcut, because the button is the only element in the window capable of having the keyboard focus. However, the button doesn’t know how to handle this command, so it will bubble up to the window, much like an input event. The window does know how to handle the command; it has declared this by creating a command binding with a handler attached to the binding’sExecuted event. This handler will be called when the user invokes the command.

    Now that we’ve seen all five features in use, we’ll examine each one in more detail.

    Please check back next week for the continuation of this series.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 1 hosted by Hostway
    Stay green...Green IT