Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • WPF : Routed Events

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 266
    Comment on it

    Introduction:

     

    WPF infrastructure provides support for routed events which allows events to tunnel down the visual tree to the leaf element or vice versa bubble up to the root element. Routed events typically appear as a pair and they don't stop routing even if they reach an event handler. Hence routed events can invoke handlers on multiple listeners in an element tree, rather than just on the object that had raised the event. In order to stop routing we have to explicitly do so in code.


    Types of Routing Events:

     

    1) Tunneling
    This type of event is raised on the root element and moves down to the element that has raised the event  via the visual tree  or till the point tunneling is explicitly stopped by handling the event  in the code. As per naming convention it is prefixed with Preview  and it is raised before the corresponding bubbling event.
     
    2) Bubbling
    This type of event is raised on the source element and it navigates up to the visual tree till it reaches the root element or it is when the event is handled in the code. The bubbling event is raised after the corresponding  tunneling event has been raised.

     

    3) Direct
    It is similar to typical .NET events wherein an event that is raised on the source element  must be handled on the same element itself.

    The routing behavior of an event ( bubbling, tunneling or direct) is determined at the stage when the event is defined.

     

    Handling the event:


    For handling an event first we need to get hold of it and then mark it as handled. All event handlers pass a "RoutedEventArgs" object. This object contains a property called Handled , all we have to do is to mark this property as true like below.

     

    e.Handled = true;

     

    Once handled it is ensured that no event handlers further along the visual tree will handle this event.

     

    Sample Program:

     

    In the following program we will use the CheckBox controls to set the value of  e.Handled to  true for event handlers ,thereby stopping the flow of events.


    Below is the XAML code for the button. Here we create the button and assign the event handlers to its PreviewMouseDown and Click events. We also need to add two CheckBoxes (chkHandleButtonPreview ,chkHandleButtonClick ) and a textbox (txtResult) for completing the user interface.

     

    <Button x:Name="btnEvent" Content="Button 1" PreviewMouseDown="btnEvent_PreviewMouseDown" Click="btnEvent_Click"  />

     


    In the event handlers we set the value for e.Handled to the checkbox state i.e checked or unchecked.

     

            private void btnEvent_PreviewMouseDown(object sender, MouseButtonEventArgs e)
            {
                e.Handled = chkHandleButtonPreview.IsChecked.Value;
                ShowMethodDetails();
            }
    
            private void btnEvent_Click(object sender, RoutedEventArgs e)
            {
                e.Handled = chkHandleButtonClick.IsChecked.Value;
                ShowMethodDetails();
            }

     


    The method  ShowMethodDetails method is called to get the name of the executing event handler

     

           private void ShowMethodDetails()
            {
                txtResult.AppendText(
                    new StackTrace(1).GetFrame(0).GetMethod().Name + '\n');
            }

    Conclusion :

     

    In this article we have learned  that  events can "tunnel", "bubble", or be simply direct. We have also learned how to handle the events and built a simple application to illustrate the same.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: