WPF Learning Path (iv) routing

Source: Internet
Author: User

Routed Events Overview

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 Routeevent class and handled by the Windows Presentation Foundation (WPF) event System.

Instance:

Xaml

<border height=" -"Width=" -"Borderbrush="Gray"borderthickness="1"> <stackpanel background="Lightgray"orientation="Horizontal"button.click="Commonclickhandler"> <button name="Yesbutton"Width="Auto">Yes</Button> <button name="Nobutton"Width="Auto">No</Button> <button name="CancelButton"Width="Auto">Cancel</Button> </StackPanel></Border>

In this simplified element tree, ClickThe source of the event is a button element, and the button clicked is the first element that has the opportunity to process the event. However, if any handler attached to the button is not used for the event, the event will bubble up to the button parent (that is, StackPanel) in the element tree. the event may bubble to Border andthen reach the page root of the element tree (not shown).

In other words, the event route for this click event is: Button-->stackpanel-->border-->, .....

Policies for routed events

Bubbling: The routed event is then routed to subsequent parent elements until the root of the element tree is reached.

Direct: Only the source element itself has the opportunity to invoke handlers to respond

Tunneling: The event handler is initially called at the root of the element tree.

The routed event policy will change because of the factors:

Event handler function

public delegate void Routedeventhandler (Object Sender, RoutedEventArgs e);
public delegate void MouseEventHandler (Object Sender, MouseEventArgs e);

RoutedEventArgs and MouseEventArgs contain information about routed events

If the "Handled" property is set to True, routed events stop passing

Class and instance event handler functions

There are two types of event handlers, the first is ordinary event handler, "instance event handler", and the other is associating an event handler and class with the Eventmanager.registerclasshandler method, "class event handler", with higher precedence

Example: (Taken from "Sunflower Treasure book--WPF self-Study Manual" 6.5)

Custom Button Class

 Public classmysimplebutton:button{StaticMysimplebutton () {Eventmanager.registerclasshandler (typeof(Mysimplebutton), Customclickevent,NewRoutedeventhandler (Customclickclasshandler),false); }     Public EventEventHandler classhandlerprocessed;  Public Static voidCustomclickclasshandler (Object sender, RoutedEventArgs e) {Mysimplebutton simpbtn= Sender asMysimplebutton; EventArgs args=NewEventArgs ();    Simpbtn.classhandlerprocessed (SIMPBTN, E); }    //Create and register routed event, routing strategies:bubble     Public Static ReadOnlyRoutedEvent customclickevent =Eventmanager.registerroutedevent ("Customclick", Routingstrategy.bubble,typeof(Routedeventhandler),typeof(Mysimplebutton)); //CLR Event Wrapper     Public EventRoutedeventhandler Customclick {add {AddHandler (customclickevent, value);}    Remove {RemoveHandler (customclickevent, value);} }    //Raise customclickevent    voidraisecustomclickevent () {RoutedEventArgs Neweventargs=NewRoutedEventArgs (mysimplebutton.customclickevent);    RaiseEvent (Neweventargs); }    //Onclick    protected Override voidOnClick () {raisecustomclickevent (); }}

MainWindow.xaml

<window x:class="Alex_wpfappdemo02.mainwindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"Xmlns:custom="clr-namespace:alex_wpfappdemo02"Custom:MySimpleButton.CustomClick="insertlist"Title="MainWindow"height=" -"Width="525"> <grid margin="3"custom:mysimplebutton.customclick="insertlist"Name="Grid1"> <Grid.RowDefinitions> <rowdefinition height="Auto"/> <rowdefinition height="*"/> <rowdefinition height="Auto"/> <rowdefinition height="Auto"/> </Grid.RowDefinitions> <custom:mysimplebutton x:name="simpbtn"customclick="insertlist">My Simple Button</custom:MySimpleButton> <listbox margin="5"Name="ListBox"grid.row="1"/> <checkbox margin="5"grid.row="2"Name="Chkhandle">Handle FirstEvent</CheckBox> <button grid.row="3"Horizontalalignment=" Right"margin="5"padding="3"click="Clear">Clear List</Button> </Grid></Window>

MainWindow.xaml.cs

protected intEventcounter =0;Private voidInsertlist (Objectsender, RoutedEventArgs e) {Eventcounter++; stringMessage ="#"+ eventcounter.tostring () +": \ r \ n"+"insertlist\r\n"+"Sender:"+ Sender. ToString () +"\ r \ n"+"Source:"+ E.source +"\ r \ n"+"Original Source:"+E.originalsource;    LISTBOX.ITEMS.ADD (message); E.handled= (BOOL) chkhandle.ischecked;}Private voidClearObjectsender, RoutedEventArgs e) {Eventcounter=0; ListBox.Items.Clear ();}

Adding class handler functions

StaticMysimplebutton () {Eventmanager.registerclasshandler (typeof(Mysimplebutton), Customclickevent,NewRoutedeventhandler (Customclickclasshandler),false);} Public EventEventHandler classhandlerprocessed; Public Static voidCustomclickclasshandler (Object sender, RoutedEventArgs e) {Mysimplebutton simpbtn= Sender asMysimplebutton; EventArgs args=NewEventArgs (); Simpbtn.classhandlerprocessed (SIMPBTN, E);}

Perfect code

 PublicMainWindow () {InitializeComponent ();  This. simpbtn.classhandlerprocessed + =NewEventHandler (Buttonraisedclass);}Private voidButtonraisedclass (Objectsender, EventArgs e) {Eventcounter++; stringMessage ="#"+ eventcounter.tostring () +": \ r \ n"+"Windows Class handler\r\n"+"Sender:"+sender.    ToString (); LISTBOX.ITEMS.ADD (message);}Private voidProcesshanlderstoo (Object sender, RoutedEventArgs e) {eventcounter++; stringMessage ="#"+ eventcounter.tostring () +": \ r \ n"+"processhanlderstoo\r\n"+"Sender:"+sender. ToString ()+"Source:"+ E.source +"\ r \ n"+"Original Source:"+E.originalsource; LISTBOX.ITEMS.ADD (message);}Private voidWindow_Loaded (Objectsender, RoutedEventArgs e) {Grid1.addhandler (mysimplebutton.customclickevent,NewRoutedeventhandler (Processhanlderstoo),true);}

You can view routes for routed events

To be continue ...

WPF Learning Path (iv) routing

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.