Saturday, 21 April 2012

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.


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"
    Title="Window4" Height="333" Width="550" Button.Click="Window_Click"
        PreviewMouseDown="Window_PreviewMouseDown"  PreviewMouseUp="Window_PreviewMouseUp">
    <Grid  PreviewMouseDown="Grid_PreviewMouseDown"  Button.Click="Grid_Click"
        <ListBox Margin="12,12,12,81" Name="listBox1" />
        <Button Margin="70,0,88,21"
                 PreviewMouseDown="btneventtest_PreviewMouseDown"  PreviewMouseUp="btneventtest_PreviewMouseUp"
                Name="btneventtest" Height="30" VerticalAlignment="Bottom">Routed Event Test</Button>

Step 3.    The Event Handlers defined in the code behind

private void Window_PreviewMouseDown(object sender,
                                                                                                                MouseButtonEventArgs e)
            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

Personal Website :-

Saturday, 7 April 2012

Routed events in Silverlight 4.0

Routed events in Silverlight 4.0

Hi friends in this article I will tell you What is routed events in Silverlight in shortly

1. What is routed Events

¡  Routed events are events with more traveling power—they can tunnel down or bubble up the element tree and be processed by event handlers along the way.
¡  Event routing allows an event to originate in one element but be raised by another one.

2. Defining, Registering, and Wrapping a Routed Event

3. Wrapping a Routed Event

4. Registering

¡  When registering an event, you need to specify the name of the event, the type of routine, the delegate that defines the syntax of the event handler, and the class that owns the event.
¡  Usually, routed events are wrapped by ordinary .NET events to make them accessible to all .NET
Languages. The event wrapper adds and removes registered callers using the Add Handler() and
Remove Handler () methods, both of which are defined in the base Framework Element class and Inherited by every WPF element.

5. Raising a Routed Event

¡  The defining class needs to raise it at some point.
¡  Exactly where this takes place is an implementation detail.
¡  However, the important detail is that your event is not raised through the traditional .NET event wrapper. Instead, you use the RaiseEvent() method that every element inherits from the UIElement class.
¡  RoutedEventArgs e = new RoutedEventArgs(ButtonBase.ClickEvent, this);
¡  The RaiseEvent() method takes care of firing the event to every caller that’s been registered with the AddHandler() method.
¡  All WPF events use the familiar .NET convention for event signatures.
¡  That first parameter of every event handler provides a reference to the object that fired the event (the sender).
¡  The second parameter is an EventArgs object that bundles together any additional details that might be important.
¡  private void img_MouseUp(object sender, MouseButtonEventArgs e)

6. Handling a Routed Event

¡  The most common approach is to add an event attribute to your XAML markup.
                <Image Source="happyface.jpg" Stretch="None“ Name="img" MouseUp="img_MouseUp" />
¡  img.MouseUp += new MouseButtonEventHandler(img_MouseUp);
¡  img.MouseUp += img_MouseUp;
¡  img.AddHandler(Image.MouseUpEvent, new MouseButtonEventHandler(img_MouseUp));

The code approach is useful if you need to dynamically create a control and attach an event handler at some point during the lifetime of your window. By comparison, the events you hook up in XAML are always attached when the window object is first instantiated. The code approach also allows you to keep your XAML simpler and more streamlined, which is perfect if you plan to share it with nonprogrammers, such as a design artist. The drawback is a significant amount of boilerplate code that will clutter up your code files.

Sujeet Bhujbal