Binding with UpdateSourceTrigger==LostFocus do not fire for Menu or Toolbar interaction
I know this is a bit old, but for any future reader, simply setting the following on my ToolBar
worked for me:
FocusManager.IsFocusScope="False"
The problem is that the TextBox does, in fact, not lose focus when the menu item is activated. Thus, the UpdateSourceTrigger LostFocus
does not fire. Depending on your (view)model, UpdateSourceTrigger PropertyChanged
might or might not be a feasible workaround.
For me, PropertyChanged
was not an option (I need to validate the data after the user finished entering it, not in between), so I used a workaround by calling this method before "Save File" (or any other menu/toolbar entry that requires an up-to-date model):
Public Shared Sub SaveFocusedTextBox()
Dim focusedTextBox = TryCast(Keyboard.FocusedElement, TextBox)
If focusedTextBox IsNot Nothing Then
Dim be = focusedTextBox.GetBindingExpression(TextBox.TextProperty)
If be IsNot Nothing Then be.UpdateSource()
End If
End Sub
A few other approaches for this problem can be found in this related question:
- WPF Databind Before Saving
(In fact, credit for this method goes to rudigrobler's answer in that question.)
This works well for me:
Private Sub MenuItem_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
Keyboard.FocusedElement.RaiseEvent(New RoutedEventArgs With {.RoutedEvent = LostFocusEvent})
End Sub