Skip to main content

Answered Question TextBox TextChanged event doesn't cause data binding to updateRSS Feed

(0)

Skute
Skute

Member

Member

222 points

156 Posts

TextBox TextChanged event doesn't cause data binding to update

I have a text box that is bound (two way) to the following property:

 private String m_description;

        public String Description
        {
            get { return m_description; }
            set
            {
                m_description = value;
                this.OnPropertyChanged("Description"); // This goes on to call the INotifyPropertyChanged.PropertyChanged event
            }
        }

But the data binding is only ever updated when the text loses focus. The TextChanged event does fire, but it doesn't  update the binding.

 I've had a look at the BindingExpression code via Reflector:

  if (this._targetObject is TextBox)
        {
            this._targetIsTextBox = true;
            TextBox box = this._targetObject as TextBox;
            box.LostFocus += new RoutedEventHandler(this.TargetTextBoxLostFocus);
            if (this._targetProperty == TextBox.TextProperty)
            {
                box.TextChanged += new TextChangedEventHandler(this.TargetTextBoxTextChanged);
            }
        }


It looks like it *should* work, but it isnt? The TargetTextBoxLostFocus function is definitely being called, I can see that in the call stack.

Any ideas?

Mark Ingram

andulvar
andulvar

Member

Member

157 points

103 Posts

Answered Question

Re: TextBox TextChanged event doesn't cause data binding to update

That, unfortunately, is the way TextBox works.  The only way to have the TextBox update a two-way binding is to cause it to lose focus.  If you want to trigger the binding on every character input, you need to focus to the TextBox's parent then back to the TextBox when you get the key press.  I can only imagine what side-effects this might have.

It's really ugly.  It would be nice to have a function on the TextBox like "UpdateBindings()" or something.  Then you could decide when to update the binding.

Your question is a variation on another question on the forum regarding updating the binding only when the user presses Enter in the TextBox.  That's not possible either.

 

Skute
Skute

Member

Member

222 points

156 Posts

Re: TextBox TextChanged event doesn't cause data binding to update

 But looking at the code in the BindingExpression class, it looks like it *should* work?

I've got around it by manually updating my data in the data context in the TextChanged method. It's not nice though.

Tomek Kmiecik
Tomek Km...

Member

Member

356 points

69 Posts

Microsoft
Answered Question

Re: TextBox TextChanged event doesn't cause data binding to update

Hey,

Currently Silverlight binding engine updates source only when TextBox loses focus, or when you set the Text property from code. When you look at TargetTextBoxChanged method, it just changes internal state of the BindingExpression, but does not write the value back to the source.

Regards,
Tomek

bogdan_vre
bogdan_vre

Member

Member

10 points

7 Posts

Re: TextBox TextChanged event doesn't cause data binding to update

I ran over the same isssue. Not sure what's best to do now...

I wonder if there is a reason for why binding was implemented like this or if it's just bad design/a slip.

jackbond
jackbond

Contributor

Contributor

2894 points

742 Posts

Re: TextBox TextChanged event doesn't cause data binding to update

bogdan_vre:

I wonder if there is a reason for why binding was implemented like this or if it's just bad design/a slip.

I'm guessing it has to do with validation. If it changed the underlying binding object on every keystroke, and there was something like a minimum length requirement, it would cause pre-mature validation errors.

brauliod
brauliod

Participant

Participant

1481 points

528 Posts

Silverlight MVP

Re: TextBox TextChanged event doesn't cause data binding to update

Mmm...

 I thought there was a way to choose whether you want in textbox to propagate the value to the binded property on focus or new character typed, seems not :-(.

 I use the same workaround TextboxChanged change the property... ugly stuff.

 Is not good that a technology forces you to follow a way for the functional flow of the app (e.g. enabling button when the textbox change not supported or just by a workaround.... you have to enable the button always and show error information in the case the textbox didn't contain what you expected :-( ).

Cheers

   Braulio

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

nelligan
nelligan

Member

Member

4 points

3 Posts

Re: TextBox TextChanged event doesn't cause data binding to update

andulvar:
The only way to have the TextBox update a two-way binding is to cause it to lose focus

You can manually trigger the update of the source by calling UpdateSource() on the BindingExpression, which can be retrieved by calling GetBindingExpression on the TextBox.

This is not a bug or an oversight. Lots of automatic features have been cut to keep the Silverlight plugin as small as possible. In this case, you can see in WPF a "PropertyChanged" member on the UpdateSourceTrigger enum. This member does not exist in Silverlight.

brauliod
brauliod

Participant

Participant

1481 points

528 Posts

Silverlight MVP

Re: Re: TextBox TextChanged event doesn't cause data binding to update

Thanks nelligan for your response...

Well... is not a bug... it's lack of functionallity. But I find it something serious beacuse it affects the functional side of an application, What if my client has an standard on validations (e.g. enable save button just when the textbox has a valida value) ? I have to say him... well... we have to use a lot of manual hacks to make this work :-(.

I thought that this was fixed /enhanced on SL 3.0, or maybe some control on the toolkit... don't know, but it's a pity having to go with workarounds to acomplish a quite standard and well know functionallity.

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

brauliod
brauliod

Participant

Participant

1481 points

528 Posts

Silverlight MVP

Re: Re: TextBox TextChanged event doesn't cause data binding to update

Well.. I have encapsulated the workaround in an user control... to make it a bit less painful

 

Well.. encapsulated in a user control the workaround...

public class TextBoxChangeExt : System.Windows.Controls.TextBox

{

public TextBoxChangeExt()

{

this.TextChanged += new TextChangedEventHandler(TextBoxChangeExt_TextChanged);

}

void TextBoxChangeExt_TextChanged(object sender, TextChangedEventArgs e)

{

// based on

// http://betaforums.silverlight.net/forums/t/103695.aspx

TextBox txCtl = (TextBox)sender;if (txCtl != null)

{

var be = txCtl.GetBindingBLOCKED EXPRESSION

{

be.UpdateSource();

}

}

}

}

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------
  • Unanswered Question
  • Answered Question
  • Announcement