WPF TextBox Binding Issue

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?

[748 byte] By [CodeGorilla] at [2008-1-5]
# 1

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?

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

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

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

be.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.

SamBent-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified