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:
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:
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.
The xaml would look like the following:
You can download the code file here in case you need it:
Hope this helps!