Fire an event when an element gains and loses focus.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:WpfApplication1"
Title="gotfocusLostfocusEffectUsingEvent" Height="400" Width="450"
>
<StackPanel>
<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
GotFocus="OnGotFocusHandler"
LostFocus="OnLostFocusHandler">Click Or Tab To Give Keyboard Focus</Button>
<Button
GotFocus="OnGotFocusHandler"
LostFocus="OnLostFocusHandler">Click Or Tab To Give Keyborad Focus</Button>
</StackPanel>
</Window>
//File:Window.xaml.vb
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Input
Namespace WpfApplication1
Public Partial Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub OnGotFocusHandler(sender As Object, e As RoutedEventArgs)
Dim tb As Button = TryCast(e.Source, Button)
tb.Background = Brushes.Red
End Sub
Private Sub OnLostFocusHandler(sender As Object, e As RoutedEventArgs)
Dim tb As Button = TryCast(e.Source, Button)
tb.Background = Brushes.White
End Sub
End Class
End Namespace
Related examples in the same category