数据绑定Windows窗体控件在失去焦点之前无法识别更改 [英] Databound Windows Forms control does not recognize change until losing focus

查看:162
本文介绍了数据绑定Windows窗体控件在失去焦点之前无法识别更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用数据绑定在C#Windows窗体客户端的文本框中显示值。当用户单击保存时,我将更改保留到数据库。但是,活动编辑器中的新值将被忽略(保存前一个值)。如果我从活动编辑器中选出,然后保存,则新值将保持不变,如预期的那样。

I use data binding to display values in text boxes in a C# Windows Forms client. When the user clicks Save, I persist my changes to the database. However, the new value in the active editor is ignored (the previous value is saved). If I tab out of the active editor, and then Save, the new value is persisted, as expected.

有没有办法强制主动控制接受其值之前坚持?

Is there a way to force the active control to accept its value before persisting?

推荐答案

如果你可以得到 绑定 对应于输入的实例( TextBox ),可以调用 WriteValue 方法将控件的值强制到绑定对象。

If you can get the Binding instance that corresponds to the input (the TextBox), you can call the WriteValue method to force the value from the control to the object it is bound to.

另外,您可以致电 EndCurrentEdit 方法 BindingManagerBase class (通常是 CurrencyManager class 实例)完成编辑,但这需要实现 ICancelAddNew IEditableObject 界面在绑定的对象上(并且不需要你去钓鱼绑定)。

Also, you can call the EndCurrentEdit method on the BindingManagerBase class (usually a CurrencyManager class instance) to finish the edit, but that requires implementation of the ICancelAddNew or IEditableObject interface on the object that is bound to (and wouldn't require you to fish for the binding).

这篇关于数据绑定Windows窗体控件在失去焦点之前无法识别更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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