WPF: Process Route events marked as handled

Source: Internet
Author: User

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;

Related Article

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.