WPF TextBox绑定问题 [英] WPF TextBox Binding Issue

查看:337
本文介绍了WPF TextBox绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天发布了一条类似的消息,但是被发布的怪物吃掉了.

如果我将TextBox的Text属性绑定到数据模型,则可以在文本框中输入文本,并且可以通过绑定魔术退出文本框后,值将出现在数据模型中.这是MSDN上描述的标准行为,并且工作正常.

当我在TextBox中包含文本并尝试使用退格键清除它时,问题就来了.清除后,如果将焦点更改为其他控件,则TextBox中剩余的空字符串不会发送到数据模型.

当TextBox的文本从任何字符串更改为空字符串时,好像不会触发属性更改事件.

这是错误还是功能?有解决方法吗?

I posted a similar message yesterday but it has been eaten by the posting monster.

If I bind TextBox's Text property to a data model, I can enter text in the text box and via the magic of binding have the value appear in the data model after I exit the text box. This is the standard behavior described on MSDN, and it works just fine.

The problem comes when I have text in the TextBox and try to use backspace to clear it. Once it is cleared, if I change the focus to a different control, the empty string left in the TextBox is not sent to the data model.

It is as though a property changed event is not fired when the TextBox's text is changed from any string to an empty string.

Is this a bug or a feature? Is there a workaround?

推荐答案

我试图重现您的问题,但对我来说很好.您的应用程序中肯定还有其他某种方式在干扰.您可以提供更多信息吗?

I tried to reproduce your problem, but it works fine for me.  There must be something else about your app that is interfering somehow.  Can you provide more information?

尝试将侦听器连接到TextBox的LostFocus事件.也许没有提高?

Try hooking up a listener to the LostFocus event of your TextBox.  Perhaps it's not getting raised?

此外,尝试在附加到TextBox的绑定表达式上显式调用UpdateSource().方法如下:

Also, try calling UpdateSource() explicitly on the binding expression attached to the TextBox.  Here's how:

BindingExpression be = BindingOperations.GetBindingExpression(myTextBox,TextBox.TextProperty);

   BindingExpression be = BindingOperations.GetBindingExpression(myTextBox, TextBox.TextProperty);

be.UpdateSource();

   be.UpdateSource();

如果引发了事件,并且UpdateSource()正确地写回了空字符串值,但是在没有调用UpdateSource的情况下没有回写该值,则说明存在干扰.关于文本框,绑定,数据项,所有这些出现在的页面等任何您可以说的内容都可以帮助您找到干扰源.

If the event is being raised and UpdateSource() correctly writes back the empty string value, but the value isn't being written back without the call to UpdateSource, then something is interfering.  Anything you can say about your textbox, the binding, the data item, the page in which all this appears, etc. might help locate the source of interference.


这篇关于WPF TextBox绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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