Understanding WPF Routed Events

Source: Internet
Author: User

(i) What when routed events
feature definition: A routed event is an event that can invoke handlers on multiple listeners in the element tree instead of just the object that raised the event.
implementation definition: A routed event is a CLR event that can be supported by an instance of the RoutedEvent class and handled by the Windows Presentation Foundation (WPF) Event System
(ii) Partitioning of routed events
(1) Bubbling: Invokes an event handler for the event source. The routed event is then routed to subsequent parent elements until the root of the element tree is reached.
(2) Tunnel: The event handler is initially invoked at the root of the element tree. The routed event is then directed toward the source node element of the routed event (that is, the element that raises the routed event), along the route to subsequent child elements.
(3) Direct: Only the source element itself has the opportunity to invoke handlers to respond. This is similar to the "Routing" of Windows forms for events. However, unlike standard CLR events, direct routed events support class handling and can be used by Eventsetter and EventTrigger.
(iii) E. Handled=true "Processed"
if Handled is set to true to mark the event as handled, the tunnel route or bubbling route will be stopped, and the route of all events handled by the class handler at one of the route points will also "stop".
(iv) The following cases are executed sequentially (all actions are click TextBlock)
(1) When the event is bubbling: Textblock-->grid-->window
(2) When the tunnel event is: Window-->grid-->textblock
about E. Application of handled=true in Sake understanding: E. After which event the handled==true is placed, the routed event after which event is not executed.

(v) Code

(1) UI code

1<window x:class="Wpfevent.mainwindow"2xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"4title="MainWindow"height=" -"Width="525"X:name="window">5<grid width=" -"height=" $"X:name="Grid"Background="Red">6<textblock x:name="TBX"Width=" -"height=" -"Background="Blue"/>7</Grid>8</Window>

(2) Backstage Code

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Windows;6 usingSystem.Windows.Controls;7 usingSystem.Windows.Data;8 usingSystem.Windows.Documents;9 usingSystem.Windows.Input;Ten usingSystem.Windows.Media; One usingSystem.Windows.Media.Imaging; A usingSystem.Windows.Navigation; - usingSystem.Windows.Shapes; -  the namespacewpfevent - { -     //(i) What when routed events -     //feature definition: A routed event is an event that can invoke handlers on multiple listeners in the element tree instead of just the object that raised the event.  +     //implementation definition: A routed event is a CLR event that can be supported by an instance of the RoutedEvent class and handled by the Windows Presentation Foundation (WPF) Event System -     //(ii) Partitioning of routed events +     //(1) Bubbling: Invokes an event handler for the event source. The routed event is then routed to subsequent parent elements until the root of the element tree is reached.  A     //(2) Tunnel: The event handler is initially invoked at the root of the element tree. The routed event is then directed toward the source node element of the routed event (that is, the element that raises the routed event), along the route to subsequent child elements.  at     //(3) Direct: Only the source element itself has the opportunity to invoke handlers to respond. This is similar to the "Routing" of Windows forms for events. However, unlike standard CLR events, direct routed events support class handling and can be used by Eventsetter and EventTrigger.  -     //(iii) E. Handled=true "Processed" -     //if Handled is set to true to mark the event as handled, the tunnel route or bubbling route will be stopped, and the route of all events handled by the class handler at one of the route points will also "stop".  -     //(iv) The following cases are executed sequentially (all actions are click TextBlock) -     //(1) When the event is bubbling: Textblock-->grid-->window -     //(2) When the tunnel event is: Window-->grid-->textblock in     //about E. Application of handled=true in Sake understanding: E.   After which event the handled==true is placed, the routed event after which event is not executed.  -     /// <summary> to     ///the interactive logic of MainWindow.xaml +     /// </summary> -      Public Partial classMainwindow:window the     { *          PublicMainWindow () $         {Panax Notoginseng InitializeComponent (); -              This. Grid. MouseLeftButtonDown + =NewMousebuttoneventhandler (grid_mouseleftbuttondown); the              This. TBX. MouseLeftButtonDown + =NewMousebuttoneventhandler (tbx_mouseleftbuttondown); +              This. MouseLeftButtonDown + =NewMousebuttoneventhandler (window_mouseleftbuttondown); A  the             //This.grid.PreviewMouseLeftButtonDown + = new Mousebuttoneventhandler (grid_previewmouseleftbuttondown); +             //this.tbx.previewmouseleftbuttondown+=new Mousebuttoneventhandler (tbx_previewmouseleftbuttondown); -             //This . Previewmouseleftbuttondown + = new Mousebuttoneventhandler (window_previewmouseleftbuttondown); $  $         } -  -        the       -         Private voidGrid_mouseleftbuttondown (Objectsender, MouseButtonEventArgs e)Wuyi         { theMessageBox.Show ("I'm the grid .");  -         } Wu  -         Private voidWindow_mouseleftbuttondown (Objectsender, MouseButtonEventArgs e) About         { $MessageBox.Show ("I am a form"); -             -         } -  A         Private voidTbx_mouseleftbuttondown (Objectsender, MouseButtonEventArgs e) +         { theMessageBox.Show ("I'm TextBlock ."); -e.handled =true; $             the         } the  the  the  -         Private voidWindow_previewmouseleftbuttondown (Objectsender, MouseButtonEventArgs e) in         { theMessageBox.Show ("I am a form"); the             About  the         } the  the         Private voidGrid_previewmouseleftbuttondown (Objectsender, MouseButtonEventArgs e) +         { -MessageBox.Show ("I'm the grid ."); the             //e.handled = true;Bayi            the         } the  -         Private voidTbx_previewmouseleftbuttondown (Objectsender, MouseButtonEventArgs e) -         { theMessageBox.Show ("I'm TextBlock ."); the             the         } the        -     } the}

Understanding WPF Routed Events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.