views:

354

answers:

3

When I bind a TextBox control to a string property of a object using two way binding, it works fine - as long as the user moves off the control before he closes the browser window/tab.

But what about the user who makes a change to the contents of a text box then closes the window expecting the data to have been saved?

While it is possible to hook into the TextChanged event or Application_Exit() handler and manually update the property, you're essentially re-doing the work of the binder. Nevertheless these seem to be the only solutions so far.

nb. The same xaml/code in a WPF app works fine (App.OnExit shows updated data in object).

A: 

Does Silverlight's Binding class have an UpdateSourceTrigger property? In WPF you can tell a control to update its bound source whenever the property changes (rather than when the control loses focus), like this:

<TextBox Text="{Binding Path=Foo,UpdateSourceTrigger=PropertyChanged}" />
Matt Hamilton
Nice try, I can't find it documented and when I tried it, the page did not load at all.By the way, I tested my original xaml/code in a WPF app and it works fine.
Phil Bachmann
+1  A: 

I'm making an educated guess here based on significant web development experience but very limited Silverlight experience.

You could use a bit of Javascript to hook into onunload in the HTML and then call a function in your Silverlight code to handle it.

17 of 26
Silverlight has a a page unload type event (app_exit), but this still requires the manual retrieval of the control data.Probably the best work-around yet.
Phil Bachmann
+1  A: 

It seems that this is problem with Silverlight. It does not update bound property on text box until it loses focus.

One workaround I had to use (I was implementing dynamic filter) is to implement TextChanged event handler and update backing property manually.

Srdjan Jovcic
Thanks for that. I've updated the question accordingly.
Phil Bachmann