Thursday, November 6, 2014

Tunneling events in WPF application using c#

11:23 AM - By azeheruddin khan 0

Explanation: - The event which is handled by the event handler of the root element followed by all other event handlers up to the source element of the element tree is called as tunneling event. In the above given program, the event called as PreviewMouseWheel is a tunneling event as it is handled by the event handler of the root element (Window) first, followed by that of Grid and then by the event handler of Cancel Button. This PreviewMouseWheel event is just opposite of MouseWheel event.

Program to show tunneling events

Page1.xaml

<Page x:Class="WPF_examples.Page1" Mouse.PreviewMouseWheel="Page_PreviewMouseWheel"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
 Title="Page1">

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

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()
        {
            InitializeComponent();
        }
        private void cancel_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            MessageBox.Show("the mouse wheel is rotated on cancel button");
        }

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

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

OutPut:



About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net 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

0 comments:

adsense

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