与UpdateSourceTrigger绑定== LostFocus不触发菜单或工具栏交互 [英] Binding with UpdateSourceTrigger==LostFocus do not fire for Menu or Toolbar interaction

查看:365
本文介绍了与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:

  • WPF Databind Before Saving

(实际上,这个方法的信用是在这个问题上给了rudigrobler的答案)

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆