Routedeventargs of a routing event has a handled attribute. When the receiver sets the handled attribute to true, the event is marked as handled, and the event source no longer sends the event. In fact, events are still sent, because many of the core components of WPF rely on routing events to be transferred on the visual tree. However, the handled event does not trigger the default event processing method. This article describes how to register an event processing method (eventhandler) that can handle "handled routing events ).
The first method is uielement. addhandler. Note that the iinputelement interface defines the standard method addhandler:
VoidAddhandler (RoutedeventRoutedevent,DelegateHandler );
The uielement inherits the iinputelement method and provides another overload:
Public VoidAddhandler (RoutedeventRoutedevent,DelegateHandler,BoolHandledeventstoo );
If the third parameter handledeventstoo is true, the processed event can be processed.
For example:
Button.Addhandler (Button.Clickevent ,(Routedeventhandler) Button_click );
Another method is eventsetter in XAML, which has a handledeventstoo attribute. However, this attribute cannot be set in XAML.CodeSettings (interpreted here: http://msdn.microsoft.com/zh-cn/library/system.windows.eventsetter.handledeventstoo.aspx), and this property also has a feature Declaration for [editorbrowsable (editorbrowsablestate. Never)], so this property is not seen in the list of Visual Studio members.
The first method is recommended, and the second method is too troublesome.
Reference code:
VaRStyle= New Style(Typeof(Button));
VaRSetter= New Eventsetter(Button.Clickevent ,(Routedeventhandler) Button_click );
Setter.Handledeventstoo= True;
button . style = style;