Event Routing in WPF
Hi
friends,
In
this article you will know how to do event routing in WPF .
1.
What is
event
Events are
messages that are send by an Object to notify the code that something occur
There are so
many events in WPF like MouseEnter,MouseLeave , MouseMove etc
2. WPF Event
Handling
With WPF you can assign the event handler
either wih XAML or in the Code Behid
The Event handling mechanism for WPF is based
on .NET events
There is new concept like Routed event
There are 3 types of event routing in WPF
1. Direct
events
2. Bubbling
3. Tunneling
1.
Direct event:
- These are like ordinary .NET events.
- They originate in one element and don’t
pass to any other.
- For example, MouseEnter (which fires when the mouse pointer moves over
- an element) is a direct event.
2. Bubbling
events.
§ These events travel up the containment
hierarchy.
§ For example, MouseDown is a bubbling event.
§ It’s raised first by the element that is
clicked. Next, it’s raised by that element’s parent, then by that element’s
parent, and so on, until WPF reaches the top of
the element tree.
3. Tunneling
events.
§ These events travel down the containment
hierarchy.
§ They give
you the chance to preview (and possibly stop) an event before it reaches the
appropriate control.
§ For example, PreviewKeyDown allows you to intercept
a key press, first at the window level and then in increasingly more specific
containers until you
reach the element that had focus when the key was pressed.
§ When there is Preview Keyword always
Tunneling is done
§ The tunneling event always fires before the
bubbling event.
§ To make life more interesting, if you mark
the tunneling event as handled, the bubbling event won’t occur. That’s because
the two events share the same instance of the RoutedEventArgs class.
Example:
Step 1. Add
a new WPF Window Project => Add Window, Name it RoutedEventDemo.xaml
Step 2. Put
the following xaml
Note: Preivew and Click events are being handled at all
levels
<Window x:Class="IGatePatniApp.RoutedEventDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window4" Height="333" Width="550" Button.Click="Window_Click"
PreviewMouseDown="Window_PreviewMouseDown" PreviewMouseUp="Window_PreviewMouseUp">
<Grid PreviewMouseDown="Grid_PreviewMouseDown" Button.Click="Grid_Click"
PreviewMouseUp="Grid_PreviewMouseUp">
<ListBox Margin="12,12,12,81" Name="listBox1" />
<Button Margin="70,0,88,21"
PreviewMouseDown="btneventtest_PreviewMouseDown" PreviewMouseUp="btneventtest_PreviewMouseUp"
Click="btneventtest_Click"
Name="btneventtest" Height="30" VerticalAlignment="Bottom">Routed Event Test</Button>
</Grid>
</Window>
Step 3.
The Event Handlers defined in the code behind
private void Window_PreviewMouseDown(object sender,
MouseButtonEventArgs e)
{
listBox1.Items.Clear();
listBox1.Items.Add("Window " + e.RoutedEvent.Name);
}
private void Window_PreviewMouseUp(object sender,
MouseButtonEventArgs e)
{
listBox1.Items.Add("Window " + e.RoutedEvent.Name);
}
private void Grid_PreviewMouseDown(object sender,
MouseButtonEventArgs e)
{
listBox1.Items.Add("Grid " + e.RoutedEvent.Name);
}
private void Grid_PreviewMouseUp(object sender,
MouseButtonEventArgs e)
{
listBox1.Items.Add("Grid " + e.RoutedEvent.Name);
}
private void Grid_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Add("Grid " + e.RoutedEvent.Name);
}
private void btneventtest_PreviewMouseDown(object sender,
MouseButtonEventArgs e)
{
listBox1.Items.Add("Button " + e.RoutedEvent.Name);
}
private void btneventtest_PreviewMouseUp(object sender,
MouseButtonEventArgs e)
{
listBox1.Items.Add("Button " + e.RoutedEvent.Name);
}
private void btneventtest_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Add("Button " + e.RoutedEvent.Name);
}
private void Window_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Add("Window " + e.RoutedEvent.Name);
}
|
Step 4.
Make this page as startup and run. click on the button and see the list
of
events in the listbox.
-
After clicking button do not click anywhere because listbox will be reloaded
with the
new set of events
Good luck and please, let me know your
feedback!
If you have any query mail me to Sujeet.bhujbal@gmail.com
If you have any query mail me to Sujeet.bhujbal@gmail.com
Regards
Sujeet Bhujbal
------------------------------------------------------------------------------------------------
Blog: www.sujitbhujbal.blogspot.com
Personal Website :- http://sujitbhujbal.wordpress.com/
Blog: www.sujitbhujbal.blogspot.com
Personal Website :- http://sujitbhujbal.wordpress.com/
Facebook
:- www.facebook.com/sujit.bhujbal
Linkedin
:- http://in.linkedin.com/in/sujitbhujbal
---------------------------------------------------------------------------------
No comments:
Post a Comment