views:

286

answers:

1

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.

+2  A: 

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:

FirstTextBox.GetBindingExpression(TextBox.TextProperty).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

Sorskoot
I haven't tried it yet but I'm sure it'll work. Thank you.
Matt