Binding update on TextBox.TextChanged event using Behaviors

In Silverlight 3.0 we have the UpdateSourceTrigger property for the Binding object which enables us to define when to save the data back to the underlying (data)property through the binding.

UpdateSourceTrigger has 2 mode:

  1. Default
  2. Explicit

Well, if it comes to TextBox it is really annoying that the update only occurs after the control loses focus. (The default setting for UpdateSourceTrigger is basically LostFocus) Now in WPF you have the UpdateSourceTrigger.PropertyChanged enumeration value which updates the data every time  the TextChanged event fires. Silverlight 3.0 doesn’t support this mode.

Now a workaround could be to implement a Behavior for TextBox. The code should be somehting like this:

image

Little explanation:

When a Behavior gets attached to the object, which in this case is limited to a TextBox, the OnAttached() method runs.

In the implementation of this method we subscribe to the TextChanged event. AssociatedObject is the textbox itself. Now when the TextChanged event fires our AssociatedObject_TextChanged eventhandler method runs. This method will get the binding defined on the TextBox’s Text property and will explicitly update the source property.

The OnDetaching() method runs if we remove the Behavior. In our case this means we have to unsubscribe from the TextChanged event.

Now lets go to Expression Blend 3.0. On the assets panel browse our new UpdateOnTextChangedBehavior and drag and drop in on the textbox. And that’s it. It’s just working.

image

The xaml would look like the following:

image

You can download the code file here in case you need it:

http://cid-6a68a5d249fe7d3a.skydrive.live.com/embedrowdetail.aspx/Public/BehaviorLibary/UpdateOnTextChangedBehavior.cs

Hope this helps!

Advertisements

10 thoughts on “Binding update on TextBox.TextChanged event using Behaviors

  1. Great find! Thanks for sharing this info – nice explanation and walk-through. Writing the same functionality through standard attached property would have been a pain.

  2. Hey Zoltan

    Thanks for sharing this code, it would have been a pain for me to research this (Silverlight Newbie) but your code and explaination was all that was needed.

    Excellent post, thanks again.

    Mark.

    • I’m using a Watermarked Textbox (looking at moving to the tkoolit PhoneTextbox) to give a similar feel to the phone textbox controls with the title watermarked within.Plus, trivially, I’m also using a version of Jeff Wilcox’s HighlightingTextBlock to give highlighted search results like searching in the People hub.

  3. Hi Kunal,Explanation is very nice. I have downloaded and test the code. Its petercfly working for Button style setter but not for Listbox. I tried to debug the binding; MyCollection is null; I have assigned the collection of Person to MyCollection.

  4. Pingback: RelayCommand in ViewModel: invocation does not have access to bound UI data in ViewModel | PHP Developer Resource

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s