在非对焦控件上执行某些操作时强制执行更新 [英] Force binding update when doing something on a non-focusable control

查看:134
本文介绍了在非对焦控件上执行某些操作时强制执行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常优雅的解决方案的问题,并且想知道是否有更好的解决方案。



我有一个视图可能有文本框只能在丢失焦点时更新其绑定(其绑定属性使用 UpdateSourceTrigger = LostFocus )。这是几乎正确...我可以将绑定的 UpdateSourceTrigger 设置为 PropertyChanged ,我不会有问题和一切都将按预期的方式工作。但是,在更新这些绑定属性时会遇到一些潜在的计算上昂贵的东西(涉及对编辑对象的深度检查,可能会长时间),所以我其实只想更新绑定在完成编辑之后。



这是工具栏的问题,因为它们的按钮不可对焦,因此点击它们(并发出命令) t实际上使文本框失去焦点,所以当命令执行时,绑定没有更新(想到实体编辑视图,使用工具栏保存按钮,当点击调用实际保存实体的保存命令时)在这种情况下,实体将被保存在文本框的值之前它失去了焦点)



我可以检查绑定在提出命令和更新源之前(这是我现在在做的),但是这意味着:




  • 有权访问到命令正在执行的绑定(或控件)。为了解决方案的完全不安,这被丢弃。命令动作在某些其他库中定义,这些应用程序应该与WPF无关。

  • 在代码隐藏事件处理程序上执行命令并执行绑定更新(或者将焦点设置为某事否则,让WPF更新源),然后再提高命令。这是我现在在做的,这是我不喜欢的(如果有其他解决方案,我宁愿将命令直接指向工具栏按钮)。

  • 拥有View界面有一个ForceEndEdit(),View执行,并在执行一些可能会导致此问题的操作时调用它。我觉得这很奇怪,不愿意这么做。



有没有办法告诉WPF更新绑定每当用户调用命令 - 或者点击一个按钮 - 控制之外就不一定会失焦?如果没有,你有没有找到任何解决方案,这个问题比上面提出的更优雅,我可能没有想到?



当我说,触发绑定更新 OnPropertyChanged (这是我所看到的提出类似的 - 虽然不是相同的问题)在这种特殊情况下不是一个很好的解决方案。 / p>

PS :这不仅仅是对于文本框,而是任何类型的编辑控件(日期选择器,范围选择器等)和这些控件作为第三方,我不一定能访问他们的源代码。



PS2 :我正在使用.NET 4.5 OnPropertyChanged()中使用$ $做一些计算费用昂贵的东西,那么$ $ $ $ code> UpdateSourceTrigger = PropertyChanged 您应该考虑在 Binding 中使用延迟,以便绑定只有用户在控件中停止输入值才能更新。



这可以解决您的问题,因为它是基于时间互动的,而不是依赖于其他事件/发生之前启动更新。这个属性是.NET 4.5中的新特性,这就是为什么我问你正在使用的.NET版本。


I have a problem which I've solved in a non-elegant way, and was wondering if there's any better solution.

I have a View which may have textboxes that only update their binding when losing focus (their bound properties use UpdateSourceTrigger=LostFocus). This is "almost" correct... I could set the binding's UpdateSourceTrigger to PropertyChanged and I wouldn't have a problem and everything would work as expected... however, there is some potentially computationally expensive stuff happening on the updating of those bound properties (involving deep checkings on the edited object which could potentially get long) so I actually only want to update the binding after I'm done with the editing.

This is posing problem with toolbars, since their buttons are not focusable, so clicking on them (and issuing the command) doesn't actually make the textbox lose focus so when the command is executed, the binding hasn't updated (think of a entity editing view, with a toolbar 'Save' button, that when clicked calls a save command which actually saves the entity. In this case, the entity would be saved with the value of the textbox before it lost the focus)

I could check the bindings before raising the command and update the source (this is what I'm doing now), however that means, either:

  • Having access to the bindings (or the controls) where the command is executing. This is discarded for the complete unelegance of the solution. The command action is defined on some other library which should be WPF-agnostic.
  • Executing the command on a code-behind event handler and perform the binding update (or just set the focus to something else and let WPF update the source) before raising the command. This is what I'm doing now and it's what I don't like (I'd prefer assigning the command directly to the toolbar button if there's other solution).
  • Have the View interface have a "ForceEndEdit()" which the View executes and call it whenever I'm performing some operation which might pose this problem. I find this rather odd and would prefer not to do it.

Is there any way to tell WPF to update the bindings "whenever the user calls a command -or clicks a button- outside the control not necessarily losing focus"? In case there's not, is there any solution any of you have found to this problem that is more elegant than the proposed above and I might have not thought of?

As I said, triggering the binding update OnPropertyChanged (which is what I've seen proposed to similar -though not identical- questions) is not a good enough solution in this particular case.

PS: this would not be only for textboxes, but any kind of editing control (datepickers, range pickers, etc.) and those controls might be third-party and I wouldn't necessarily have access to their source code.

PS2: I'm using .NET 4.5

解决方案

If you are doing something computationally expensive during OnPropertyChanged() with UpdateSourceTrigger=PropertyChanged you should consider using Delay in the Binding so that the Binding updates only once the user has stopped entering a value in the control.

This could solve your problem because it's interaction time-based rather than relying on some other event/occurance before initiating the update. This property is new in .NET 4.5 which is why I asked what version of .NET you were using.

这篇关于在非对焦控件上执行某些操作时强制执行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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