Use uielement. addhandler to capture the routedevent that has been processed

Source: Internet
Author: User
Uielement. addhandler method (routedevent, delegate)

Add the specified route event to the processing set of the current element by the event handler.

Remarks

The same handler for multiple epochs of the same event without exception. However, when processing an event, the handler actually calls it multiple times. Therefore, consider how this behavior may have side effects that should be considered in your processing program.

This method is usually used to provide an accesser that implements the Microsoft. NET event access mode for "add" Custom routing events.

Msdn source code:

// Abstract: // Add a route event handler for the specified route event and add the handler to the processing assembly of the current element. When handledeventstoo is set to true, the provided handler can be called for a route event that has been marked as a route event processed by other elements during Event Routing. //// Parameter: // routedevent: // The identifier of the route event to be processed. //// Handler: // a reference to the handler implementation. //// Handledeventstoo: // if it is true, the handler will be registered as follows: This handler will be called even if the routing event is marked as handled in the event data; if this parameter is set to false, the default condition is used to register the handler. That is, when a route event is marked as handled, the handler is not called. The default value is // false. Do not routinely request to reprocess routing events. For more information, see "Remarks ". //// Exception: // system. argumentnullexception: // routedevent or handler is null. // system. argumentexception: // routedevent does not indicate a supported route event. -Or-handler does not support delegation. /// System. notimplementedexception: // try to add a handler for events not supported by the current platform variant. Public void addhandler (routedevent, delegate handler, bool handledeventstoo );

 

1. Self-simple code implementation through Button:

button1.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(myListBox_MouseLeftButtonDown), true);public void myListBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){MessageBox.Show("Look");}

 

2. Custom events Support Event Routing

public class MyButtonSimple: Button{    // Create a custom routed event by first registering a RoutedEventID    // This event uses the bubbling routing strategy    public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(        "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));    // Provide CLR accessors for the event    public event RoutedEventHandler Tap    {            add { AddHandler(TapEvent, value); }             remove { RemoveHandler(TapEvent, value); }    }    // This method raises the Tap event    void RaiseTapEvent()    {            RoutedEventArgs newEventArgs = new RoutedEventArgs(MyButtonSimple.TapEvent);            RaiseEvent(newEventArgs);    }    // For demonstration purposes we raise the event when the MyButtonSimple is clicked    protected override void OnClick()    {        RaiseTapEvent();    }}
<Window      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:custom="clr-namespace:SDKSample;assembly=SDKSampleLibrary"    x:Class="SDKSample.RoutedEventCustomApp"    >    <Window.Resources>      <Style TargetType="{x:Type custom:MyButtonSimple}">        <Setter Property="Height" Value="20"/>        <Setter Property="Width" Value="250"/>        <Setter Property="HorizontalAlignment" Value="Left"/>        <Setter Property="Background" Value="#808080"/>      </Style>    </Window.Resources>    <StackPanel Background="LightGray">        <custom:MyButtonSimple Name="mybtnsimple" Tap="TapHandler">Click to see Tap custom event work</custom:MyButtonSimple>    </StackPanel></Window>

 

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.