Event Handling in WPF

Kailash Chandra Behera | Tuesday, July 26, 2016

Introduction

Event handling in WPF is different as compare to Winform and Asp.net, this article describes the type of events WPF supports.

Getting Started

As compare to classic WinForm and ASP.NET, event handling is different in WPF. Because Winform and ASP.NET support Direct Events and WPF supports Routed Events, in direct event if an event is raised by any control, that event must be handled by the same control itself. even if the event is raised in a particular control, it is not necessary to handle on that exact target control itself, instead it can be handled in it’s any one of the parent element also. This type of events are called Routed Events

Routed event can be attached in any control even that control does not have that events declared on that class, Hence Routed Events are called Attached Events. For example, WPF Grid does not have Click event, but if the Grid has button inside, then the click event of the button can be handled by the Stack panel.

Event Attachment syntax in XAML

 <Grid Name="mygrid" Button.Click="Grid_Click">  
  </Grid>  

Event Attachment syntax in Code Behind

 myGrid.AddHandler(Button.ClickEvent, new RoutedEventHandler(Grid_Click));  

Example

 //In xaml  
 <Grid Name="mygrid" Button.Click="Grid_Click">  
     <Button x:Name="button" Content="Attached Event Example" HorizontalAlignment="Left" Margin="199,136,0,0" VerticalAlignment="Top" Width="104" Height="53"/>  
   </Grid>  
 //Code behind  
 myGrid.AddHandler(Button.ClickEvent, new RoutedEventHandler(Grid_Click));  
 // Handling Event  
 private void Grid_Click(object sender, RoutedEventArgs e)  
 {  
       MessageBox.Show("Button Click event handled in the grid");  
  }  

Types of Routed Event

Based on the routing strategies WPF Routed Event classified into following three types.
  1. Bubbling Event:

    When this event is raised, first the event will be raised in the target element and then it will be bubbling up to the top most parent, that means event handlers on the event source are invoked first and the event then routes to successive parent elements until reaching the element tree root. Most routed events use the bubbling routing strategy. Bubbling routed events are generally used to report input or state changes from distinct controls or other UI elements.

    MouseDown and MouseUp are most used bubbling events in WPF, see the example for more

     <Grid Name="mygrid" MouseDown="mygrid_MouseDown">  
         <TextBlock x:Name="textBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" MouseDown="TextBlock_MouseDown" Text="Bubbling event example" VerticalAlignment="Top" Width="120" Margin="193,154,0,0"/>  
       </Grid>  
    
      private void mygrid_MouseDown(object sender, MouseButtonEventArgs e)  
      {  
           MessageBox.Show("Mouse down handled in the grid");  
      }  
      private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)  
      {  
           MessageBox.Show("Mouse down handled in textblock");  
     }  
    

    In the above example if you mouse down on the Text Block, first the MouseDown event will be raised in the target Text Block then in Grid .

  2. Tunneling Event:

    This events are just opposite to Bubbling Events, the event first will be raised on the top most parent of the control on which the event is originally raised. From there, it will traverse to the target control like a tunneling.

     <Grid Name="mygrid" PreviewMouseDown="mygrid_PreviewMouseDown">  
         <TextBlock x:Name="textBox" PreviewMouseDown="textBox_PreviewMouseDown" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Bubbling event example" VerticalAlignment="Top" Width="120" Margin="193,154,0,0"/>  
      </Grid>  
    
     private void mygrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)  
     {  
           MessageBox.Show("Preview Mouse Down handled in the grid");  
     }  
     private void textBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)  
     {  
           MessageBox.Show("Preview Mouse Down handled in TextBlock");  
     }  
    

    In the above example when you mouse down on the Text Block, first the PreviewMouseDown event will be raised in the Grid first then in the target Text Block. This type of event will be raised by Preview event, below are the list of most used Tunneling Events.

    • PreviewMouseDown
    • PreviewMouseUp
    • PreviewKeyUp
    • PreviewKeyDown

  3. Direct Event:

    WPF also supports Direct Event unlike classic WinForm, a routed event can be converted into direct event in WPF. When bubbling or tunneling is not required, then you may use e.Handled = true with in handler method to work Routed Event as Direct Event. This will stop the event from rising to the parent or child base on the event type.

Related Articles

  1. WPF Dispatcher
  2. Command In MVVM
  3. Resource in WPF
  4. WPF Style
  5. Data Validation in WPF
  6. UpdateSourceTrigger
  7. WPF Event Trigger
  8. WPF Binding Mode

Summary

In the above of this article, we have discussed the events are supported by WPF, hope this article may helpful to you.

Thanks