Wednesday, January 11, 2017

How to stop routed event?

We have following xaml
 x:Class = "WPFRoutedEvents.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "450" Width = "604" ButtonBase.Click  = "Window_Click" >
 
    
       Margin = "20" ButtonBase.Click = "StackPanel_Click">             
          
   

If you want to stop the routed event at any particular level, then you will need to set the e.Handled = true;
Let’s change the StackPanel_Click event as shown below −
private void StackPanel_Click(object sender, RoutedEventArgs e) 
{
 txt2.Text = "Click event is bubbled to Stack Panel"; 
e.Handled = true; 
}


Before Stopping the output will be

Button is Clicked
Click event is bubbled to Stack Panel
Click event is bubbled to Window

After Stopping the output

Button is Clicked
Click event is bubbled to Stack Panel


No comments:

Followers

Link