Mega Code Archive

 
Categories / VB.Net Tutorial / WPF
 

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

<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.vb Imports System Imports System.Text Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Documents Namespace WpfApplication1   Public Partial Class RoutedEventHandle     Inherits StackPanel     Private Sub HandleClick(sender As Object, args As RoutedEventArgs)       Dim fe As FrameworkElement = DirectCast(sender, FrameworkElement)       Console.WriteLine("Event handled by element named ")       Console.WriteLine(fe.Name)       Dim fe2 As FrameworkElement = DirectCast(args.Source, FrameworkElement)       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)     End Sub   End Class End Namespace