Bubble routed events, and write an event handler for a routed event.

image_pdfimage_print


   
  

<StackPanel
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.RoutedEventHandle" Name="dpanel" Button.Click="HandleClick">
  <StackPanel.Resources>
      <Style TargetType="{x:Type Button}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
  </StackPanel.Resources>
  <Button Name="Button1">Item 1</Button>
  <Button Name="Button2">Item 2</Button>
</StackPanel>
//File:Window.xaml.cs

using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WpfApplication1
{
    public partial class RoutedEventHandle : StackPanel
    {
        void HandleClick(object sender, RoutedEventArgs args)
        {
            FrameworkElement fe = (FrameworkElement)sender;
            Console.WriteLine("Event handled by element named ");
            Console.WriteLine(fe.Name);
            FrameworkElement fe2 = (FrameworkElement)args.Source;
            Console.WriteLine("Event originated from source element of type ");
            Console.WriteLine(args.Source.GetType().ToString());
            Console.WriteLine(" with Name ");
            Console.WriteLine(fe2.Name);
            Console.WriteLine(args.RoutedEvent.RoutingStrategy);
        }
    }
}