Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Binding Update Silverlight

How can I force my objects DataContext bindings to update? I'm using an event on a grid, and binding updates are not being processed before my event fires.

Any cheap tricks to get around this? In the end I can always do things the old manual way of getting the values from my textboxes and updating my object, but it'd be nice to have binding do it for me.

UPDATE

My grid contains two textboxes. If a user clicks on the grid (MouseButtonUp event) then I save the changes. But in my MouseButtonUp event handler, the datacontext is not up to date yet. I'd imagine it's because a text box only updates when focus is lost.

like image 440
Matt Avatar asked Apr 16 '10 21:04

Matt


1 Answers

You can force an update of the source a binding by calling the UpdateSource() methond on on the binding. Try adding it to the MouseButtonUp event handler just before saving.

Like so:

BindingExpression binding = FirstTextBox.GetBindingExpression(TextBox.TextProperty)
if (null != binding) binding.UpdateSource();

You can find more information about this on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingexpression.updatesource(VS.95).aspx

like image 81
Sorskoot Avatar answered Oct 29 '22 03:10

Sorskoot