Force Binding Update Silverlight

自作多情 提交于 2020-01-01 04:07:08

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/2656254/force-binding-update-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!