Thursday, November 6, 2014

Bubbling events in WPF application using C#

11:24 AM - By azeheruddin khan 0

Explanation: - Windows Presentation Foundation (WPF) is used for building standalone and browser-hosted applications which are resolution-independent in order to take advantage of the modern graphics hardware. WPF uses an XML-based markup language which is used to develop the GUI for an application. It is used to create windows, dialog boxes, pages, and user controls, and to fill them with controls, shapes, and graphics.

An event is an action performed by a user. For ex: - pressing and releasing a mouse button, pressing and releasing a key on the keyboard etc. These events are performed on the controls like buttons and textboxes etc. In the above program, an event called ‘MouseWheel’ which is raised or notified when the user rotates the mouse wheel over a control.

In WPF, a set of controls like button, textbox and window etc are called elements which form an element tree. The container element like Window containing all the elements in it is the root element and the element on which an event is performed is the Source element. A delegate which links both the event and eventhandler is called Listener which receives information about the raised event and passes it to the respective event handler which gives an appropriate response.

An event which invokes an event handler based on a listener in an element tree is called Routed event. Routed events are of three types:-

1)    Direct events
2)    Bubbling events
3)    Tunneling events

The event which is handled by the event handler of the source element followed by all other event handlers up to the root element of the element tree is called as bubbling event.

 As shown in the above given program, the event: MouseWheel is a bubbling event as it is handled by the eventhandler of the source element: cancel button followed by eventhandler of Grid and then by eventhandler of the root element: Window.

Program to show Bubbling events


<Page x:Class="WPF_examples.Page1" Mouse.MouseWheel="Page_MouseWheel"
      d:DesignHeight="300" d:DesignWidth="300"

    <Grid Height="102" Width="209" Mouse.MouseWheel="Grid_MouseWheel">
        <Button Content="CANCEL" Name="cancel"  Height="48" HorizontalAlignment="Left" Margin="43,32,0,0" VerticalAlignment="Top" Width="119" MouseWheel="cancel_MouseWheel" />

Page1.xaml.cs (code behind the xaml page)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WPF_examples
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
        public Page1()

        private void cancel_MouseWheel(object sender, MouseWheelEventArgs e)
            MessageBox.Show("the mouse wheel is rotated on cancel button");

        private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
            MessageBox.Show("the mouse wheel is rotated on the grid");

        private void Page_MouseWheel(object sender, MouseWheelEventArgs e)
            MessageBox.Show("the mouse wheel is rotated on the window");

About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, and ms sql.My work comprise of medium and enterprise level projects using and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post



© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top