开发者

Force Binding Update Silverlight

开发者 https://www.devze.com 2022-12-26 23:39 出处:网络
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.

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 开发者_JS百科to date yet. I'd imagine it's because a text box only updates when focus is lost.


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


Alternativly you can catch TextInput event (it will fire when user presses enter inside text box) and then change focus to second textBox. loosing focus will update binded value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号