.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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek