Listing Routed Event on Mouse Action

This blog lists of routed event generated by mouse in the Grid.

XAML Code

<Window x:Class="Part3_RoutedEventViewer.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Examining Routed Events" Height="300" Width="300"

WindowState="Maximized">

  <Grid x:Name="gridMain">

 

    <Grid.Resources>

 

      <LinearGradientBrush x:Key="lb" EndPoint="0.398,-0.083" StartPoint="0.398,0.688">

        <GradientStop Color="#FFFEA857" Offset="0"/>

        <GradientStop Color="#FFFFFFFF" Offset="0.745"/>

      </LinearGradientBrush>

 

      <DataTemplate x:Key="RoutedEventNameTemplate">

        <TextBlock Text="{Binding Path=RoutedEventName}" Width="auto" Margin="0,0,0,0" />

      </DataTemplate>

 

      <DataTemplate x:Key="SenderNameTemplate">

        <TextBlock Text="{Binding Path=SenderName}" Width="auto" Margin="0,0,0,0"/>

      </DataTemplate>

 

      <DataTemplate x:Key="ArgsSourceTemplate">

        <TextBlock Text="{Binding Path=ArgsSource}" Width="auto" Margin="0,0,0,0"/>

      </DataTemplate>

 

      <DataTemplate x:Key="OriginalSourceTemplate">

        <TextBlock Text="{Binding Path=OriginalSource}" Width="auto" Margin="0,0,0,0"/>

      </DataTemplate>

 

      <Style x:Key="headerContainerStyle" TargetType="{x:Type GridViewColumnHeader}">

        <Setter Property="Template">

          <Setter.Value>

            <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">

              <Grid x:Name="gd" Background="{StaticResource lb}">

                <TextBlock Text="{TemplateBinding Content}"

                FontSize="{TemplateBinding FontSize}"

                Foreground="Black"/>

              </Grid>

            </ControlTemplate>

          </Setter.Value>

        </Setter>

      </Style>

 

    </Grid.Resources>

    <Grid.RowDefinitions>

      <RowDefinition Height="auto"/>

      <RowDefinition Height="auto"/>

      <RowDefinition Height="100*"/>

    </Grid.RowDefinitions>

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">

      <Button x:Name="btnTop" Margin="10" Padding="2" Content="Examining Routed Events" Height="auto"/>

      <Button x:Name="btnClearItems" Margin="10" Padding="2" Content="Clear Items" Height="auto" Click="btnClearItems_Click"/>

    </StackPanel>

    <ListView x:Name="lvResults" Margin="0,0,0,0" IsSynchronizedWithCurrentItem="True" Grid.Row="2" >

      <ListView.View>

        <GridView ColumnHeaderContainerStyle="{StaticResource headerContainerStyle}" >

          <GridViewColumn Header="RoutedEventName" Width="150" CellTemplate="{StaticResource RoutedEventNameTemplate}"/>

          <GridViewColumn Header="SenderName" Width="100" CellTemplate="{StaticResource SenderNameTemplate}"/>

          <GridViewColumn Header="ArgsSource" Width="100" CellTemplate="{StaticResource ArgsSourceTemplate}"/>

          <GridViewColumn Header="OriginalSource" Width="100" CellTemplate="{StaticResource OriginalSourceTemplate}"/>

        </GridView>

      </ListView.View>

    </ListView>

  </Grid>

</Window>

Code Behind Codes

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace Part3_RoutedEventViewer

{

    /// <summary>

    /// Demo application that displays some data about the events

    /// that were recieved by a users actions. Which allows users

    /// to see the difference between tunneling/routed events

    /// </summary>

    public partial class Window1 : Window

    {

        #region Ctor

        /// <summary>

        /// Wires up several of the standard <see cref="FrameworkElement">

        /// FrameworkElement</see> Tunneling/Bubbling <see cref="RoutedEvent">RoutedEvents</see>.

        /// This demo also displays some data about the events that were recieved by

        /// a users actions.

        /// </summary>

        public Window1()

        {

            InitializeComponent();

            UIElement[] els = { this, gridMain, btnTop, lvResults };

            foreach (UIElement el in els)

            {

                //keyboard

                el.PreviewKeyDown += GenericHandler;

                el.PreviewKeyUp += GenericHandler;

                el.PreviewTextInput += GenericHandler;

                el.KeyDown += GenericHandler;

                el.KeyUp += GenericHandler;

                el.TextInput += GenericHandler;

 

                //Mouse

                el.MouseDown += GenericHandler;

                el.MouseUp += GenericHandler;

                el.PreviewMouseDown += GenericHandler;

                el.PreviewMouseUp += GenericHandler;

 

                //Stylus

                el.StylusDown += GenericHandler;

                el.StylusUp += GenericHandler;

                el.PreviewStylusDown += GenericHandler;

                el.PreviewStylusUp += GenericHandler;

 

                el.AddHandler(Button.ClickEvent, new RoutedEventHandler(GenericHandler));

            }

        }

        #endregion

 

        #region Private Methods

        /// <summary>

        /// Creates a new <see cref="EventDemoClass">EventDemoClass</see>

        /// to represent the <see cref="RoutedEvent">RoutedEvent</see>.

        /// And adds this new EventDemoClass to the listbox

        /// </summary>

        private void GenericHandler(object sender, RoutedEventArgs e)

        {

 

            lvResults.Items.Add(new EventDemoClass()

            {

                RoutedEventName = e.RoutedEvent.Name,

                SenderName = typeWithoutNamespace(sender),

                ArgsSource = typeWithoutNamespace(e.Source),

                OriginalSource = typeWithoutNamespace(e.OriginalSource)

            });

 

        }

        /// <summary>

        /// Returns the type name without the namespace portion

        /// </summary>

        private string typeWithoutNamespace(object obj)

        {

            string[] astr = obj.GetType().ToString().Split('.');

            return astr[astr.Length - 1];

        }

 

        /// <summary>

        /// Clears the listbox of events

        /// </summary>

        private void btnClearItems_Click(object sender, RoutedEventArgs e)

        {

            lvResults.Items.Clear();

        }

        #endregion

    }

 

    #region EventDemoClass CLASS

    /// <summary>

    /// A simpy data class that is used to display event data

    /// </summary>

    public class EventDemoClass

    {

        public string RoutedEventName { get; set; }

        public string SenderName { get; set; }

        public string ArgsSource { get; set; }

        public string OriginalSource { get; set; }

    }

    #endregion

}

 

 
 

Ebook Download
View all
Learn
View all