Linking with UpdateSourceTrigger == LostFocus does not work for interacting with a menu or toolbar

I noticed that bindings with UpdateSourceTrigger==LostFocus not updated when the user activates a menu or toolbar.

This leads to an unsuccessful situation where the last change that the user made is lost when the user selects "Save File" from the menu or toolbar.

Is there an easy way around this or do I need to change all the bindings to UpdateSourceTrigger=PropertyChanged .

+7
source share
3 answers

The problem is that the TextBox does not actually lose focus when the menu item is activated. Therefore, UpdateSourceTrigger LostFocus does not start. Depending on your view, the UpdateSourceTrigger PropertyChanged model may or may not be a valid workaround.

For me, PropertyChanged not an option (I need to check the data after the user has finished entering it, and not between them), so I used a workaround by calling this method to "Save file" (or any other menu / toolbar that requires an updated 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 

Several other approaches to this problem can be found in this related question:

(In fact, the credit for this method goes to the rudigrobler answer in this question.)

+2
source

I know this is a bit outdated, but for any future reader, the following simply worked for me:

 FocusManager.IsFocusScope="False" 
+10
source

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 
+3
source

All Articles