Routed events:
1. The three strategies typically used for routed events are as follows:
A.bubble (bubbling Mode): Events are passed from their own excitation to the root element;
B.direct (direct mode): Only event sources have the opportunity to respond to events (as with traditional events);
C.tunnel (tunnel mode): Events are passed from the root element to themselves.
In general, the input events provided by WPF are implemented as bubbling/tunneling pairs. Tunnel events are often referred to as preview events.
2. How routed events are registered
Register routed events to the event system via the EventManager registerroutedevent () function;
public static RoutedEvent registerroutedevent (string name, RoutingStrategy RoutingStrategy,
Type Handlertype, type ownertype);
Parameter explanation:
The first parameter, name, represents the name of the event in the WPF event system;
The second parameter, RoutingStrategy, indicates the routing principle of routed events.
The third parameter, Handlertype, is used to indicate the type of event handler function;
The fourth parameter, ownertype, is used to indicate the type that owns the routed event.
For example:
The code for the control class to register the MouseDoubleClick event is as follows:
public static readonly RoutedEvent mousedoubleclickevent =
Eventmanager.registerroutedevent ("MouseDoubleClick", Routingstrategy.direct,
typeof (Mousebuttoneventhandler), typeof (Control));
The EventManager class also provides a Registerclasshandler () function that registers a class handler for a specific routed event.
The function is prototyped as follows:
public static void Registerclasshandler (Type ClassType, RoutedEvent RoutedEvent,
Delegate handler, bool HandledEventsToo);
The first parameter is used to specify the type of the registration class handler function;
The second parameter is used to specify the event that the class handler needs to listen on;
The third parameter indicates the class handler function;
The fourth parameter is set to True to allow the class handler to handle routed events that are marked as handled.
3. Steps to create a custom routed event:
(1) Declaring and registering routed events;
(2) Adding CLR event wrappers for routed events;
(3) Create a method that can fire routed events.
<Windowx:class= "Wpfdemos.mainwindow"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "MainWindow"Height= " the"Width= "525"> <Gridx:name= "GridRoot" > <Gridx:name= "Grida"Margin= "Ten"Background= "Blue"> <grid.columndefinitions> <ColumnDefinition/> <ColumnDefinition/> </grid.columndefinitions> <Canvasx:name= "Canvasleft"Grid.column= "0"Background= "Red"Margin= "Ten"> <Buttonx:name= "Buttonleft"Width= "Max"Height= "+"Margin= "30,135,357,76"Content= "Left"> <Button.rendertransform> <!--Degree of rotation - <RotateTransformAngle= "+"/> </Button.rendertransform> </Button> <Canvasx:name= "Canvasright"Grid.column= "1"Background= "Yellow"Margin= "Ten"> <Buttonx:name= "Buttonright"HorizontalAlignment= "Center"Content= "Right"Width= "Max"Height= "+"Margin= "138,105,325,106" > <Button.layouttransform> <RotateTransformAngle= "All"/> </Button.layouttransform> </Button> <ButtonContent= "Test"Height= "All"HorizontalAlignment= "Left"Margin= "80,217,0,0"Name= "Buttontest"VerticalAlignment= "Top"Width= " the" > <Button.rendertransform> <!--Degree of rotation - <RotateTransformAngle= "+"/> </Button.rendertransform> </Button> </Canvas> </Canvas> </Grid> </Grid></Window>
Using system.windows;using system.windows.controls;namespace wpfdemos{//<summary>/// Interactive logic for MainWindow.xaml/// </summary> public partial class Mainwindow:window { public MainWindow () { InitializeComponent (); Consider using when there are multiple identical events for the same control in the same UI, such as the button event this in the calculator . Gridroot.addhandler (Button.clickevent, New Routedeventhandler (this. buttonclicked)); } private void ButtonClicked (Object Sender,routedeventargs e) { MessageBox.Show ("test==" + (E.originalsource As FrameworkElement). Name);}}}
WPF Demo511 Control Common events