本文介绍了与 UpdateSourceTrigger==LostFocus 绑定不会触发菜单或工具栏交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到当用户激活菜单或工具栏时,UpdateSourceTrigger==LostFocus 的绑定不会更新.

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

这会导致当用户从菜单或工具栏中选择保存文件"时用户所做的最后一次更改丢失的不幸情况.

This leads to the unfortunate situation that the last change that the user made gets lost when the user selects "Save File" from the menu or toolbar.

是否有一种简单的方法可以解决这个问题,或者我是否必须将所有绑定更改为 UpdateSourceTrigger=PropertyChanged.

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

推荐答案

问题在于 TextBox 实际上不会在菜单项被激活时失去焦点.因此,UpdateSourceTrigger LostFocus 不会触发.根据您的(视图)模型,UpdateSourceTrigger PropertyChanged 可能是也可能不是可行的解决方法.

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.

对我来说,PropertyChanged 不是一个选项(我需要在用户完成输入后验证数据,而不是在两者之间),所以我使用了一个解决方法在保存文件"(或任何其他需要最新模型的菜单/工具栏条目)之前调用此方法:

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:

(实际上,这种方法的功劳归功于 rudigrobler 在该问题中的回答.)

这篇关于与 UpdateSourceTrigger==LostFocus 绑定不会触发菜单或工具栏交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 03:09