I've created a simple WPF User Control Library. In this project there's a single class UserControl1. I've a Canvas on this control called Canvas1. The code for this class is given below.
Class UserControl1
Public Sub Draw()
Dim t As New TextBox
t.Text = "Whatever"
t.Name = "t"
t.RenderTransform = New TranslateTransform(0, 0)
Canvas1.Children.Add(t)
Dim t1 As New TextBox
t1.Text = "YetAgainWhatever"
t1.Name = "t1"
t1.RenderTransform = New TranslateTransform(0, 20)
Canvas1.Children.Add(t1)
Dim lostFocusHandler As New KeyboardFocusChangedEventHandler(AddressOf Handler)
AddHandler t.LostKeyboardFocus, lostFocusHandler
AddHandler t1.LostKeyboardFocus, lostFocusHandler
End Sub
Private Sub Handler(ByVal sender As Object, ByVal e As RoutedEventArgs)
e.Handled = True
If TypeOf sender Is TextBox Then
Dim t As TextBox = CType(sender, TextBox)
Debug.Print("LostKeyboardFocusFrom:" & t.Name)
For Each el As UIElement In Canvas1.Children
If TypeOf el Is TextBox Then
Dim t1 As TextBox = el
If t1.IsKeyboardFocusWithin Then
Debug.Print("CurrentKeyboardFocusIn: " & t1.Name)
End If
End If
Next
End If
End Sub
End Class
I've hosted this control in a windows form which has a normal(not WPF) button on it. In this form's constructor I call the draw function of the WPF control. Now when I tab through the controls on the form the event for the lost focus for text box t1 is fired twice. Once when the t1 loses keyboard focus, which is expected. But again when the non-WPF button loses focus. Has anyone any idea why could this be happening?