.NET
  Home arrow .NET arrow Page 2 - Input with Windows Presentation Foundation
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

Input with Windows Presentation Foundation
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-06-26

    Table of Contents:
  • Input with Windows Presentation Foundation
  • Routed Events, continued
  • Halting Event Routing
  • Routed Events and Normal Events

  • 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


    Input with Windows Presentation Foundation - Routed Events, continued


    (Page 2 of 4 )

    You may be wondering whether there is a meaningful difference between a direct routed event and an ordinary CLR event—after all, a direct event isn’t really routed anywhere. The main difference is that with a direct routed event, WPF provides the underlying implementation, whereas if you were to use the normal C# event syntax to declare an event, the C# compiler would provide the implementation. The C# compiler would generate a hidden private field to hold the event handler, meaning that you pay a per-object overhead for each event whether or not any handlers are attached. With WPF’s event implementation, event handlers are managed in such a way that you pay an overhead only for events to which handlers are attached. In a UI with thousands of elements each offering tens of events, most of which don’t have handlers attached, this starts to add up. Also, WPF’s event implementation offers something not available with ordinary C# events: attached events, which are described later.

    With the exception of direct events, WPF defines most routed events in pairs—one bubbling and one tunneling. The tunneling event name always begins withPreviewand is raised first. This gives parents of the target element the chance to see the event before it reaches the child (hence thePreview prefix). The tunneling preview event is followed directly by a bubbling event. In most cases, you will handle only the bubbling event—the preview would usually be used only if you wanted to be able to block the event, or if you needed a parent to do something in advance of normal handling of the event.

    In Example 4-1, most of the elements have event handlers specified for thePreviewMouseDownandMouseDownevents—the bubbling and tunneling events, respectively. Example 4-2 shows the corresponding code-behind file.

    Example 4-2. Handling events

    using System;
    using System.Windows;
    using System.Diagnostics;
     

    namespace EventRouting {
        public partial class Window1 : Window {
            public Window1() {
                InitializeComponent();
            }

            void PreviewMouseDownButton(object sender, RoutedEventArgs e)
            { Debug.WriteLine("PreviewMouseDownButton"); }

            void MouseDownButton(object sender, RoutedEventArgs e)
            { Debug.WriteLine("MouseDownButton"); }

            void PreviewMouseDownGrid(
              object sender, RoutedEventArgs e)
            { Debug.WriteLine("PreviewMouseDownGrid"); }

            void MouseDownGrid(object sender, RoutedEventArgs e)
            { Debug.WriteLine("MouseDownGrid"); }

            void PreviewMouseDownCanvas(object sender, RoutedEventArgs e)
            { Debug.WriteLine("PreviewMouseDownCanvas"); }

            void MouseDownCanvas(object sender, RoutedEventArgs e)
            { Debug.WriteLine("MouseDownCanvas"); }

            void PreviewMouseDownEllipse(object sender, RoutedEventArgs e)
            { Debug.WriteLine("PreviewMouseDownEllipse"); }

            void MouseDownEllipse(object sender, RoutedEventArgs e)
            { Debug.WriteLine("MouseDownEllipse"); }

        }
    }

    Each handler prints out a debug message. Here is the debug output we get when clicking on theEllipseinside theCanvas:

      PreviewMouseDownButton 
      PreviewMouseDownGrid
      PreviewMouseDownCanvas
      PreviewMouseDownEllipse
      MouseDownEllipse
      MouseDownCanvas
      MouseDownGrid

    This confirms that the preview event is raised first. It also shows that it starts from theButtonelement and works down, as we would expect with a tunneling event. The bubbling event that follows starts from theEllipseelement and works up. (Interestingly, it doesn’t appear to get as far as theButton. We’ll look at why this is shortly.)

    This bubbling routing offered for most events means that you can register a single event handler on a control, and it will receive events for any of the elements nested inside the control. You do not need any special handling to deal with nested content, or controls whose appearance has been customized with templates—events simply bubble up to the control and can all be handled there.

    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

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