Mega Code Archive

 
Categories / VB.Net Tutorial / WPF
 

Find source element of an element in event handler by casting

<StackPanel   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   x:Class="WpfApplication1.RoutedEventSource">   <StackPanel.Resources>     <Style TargetType ="{x:Type Button}">       <Setter Property="Height" Value="30"/>       <Setter Property="Width" Value="100"/>       <Setter Property="HorizontalAlignment" Value="Left"/>     </Style>   </StackPanel.Resources>   <Button Click="HandleClick">Button 1</Button>   <Button Click="HandleClick">Button 2</Button>   <Button Click="HandleClick">Button 3</Button>     </StackPanel> //File:Window.xaml.vb Imports System.Windows Imports System.Windows.Controls Namespace WpfApplication1   Public Partial Class RoutedEventSource     Private Sub HandleClick(sender As Object, e As RoutedEventArgs)       Dim srcButton As Button = TryCast(e.Source, Button)       srcButton.Width = 200     End Sub   End Class End Namespace