Update all bindings in UserControl at once

丶灬走出姿态 提交于 2019-12-08 17:35:57

问题


I need to update all the bindings on my UserControl when its visibility changes to Visible. Pretty much all my bindings are bound to the DataContext property of the user control so I'm trying to update the target of that binding:

BindingOperations.GetBindingExpressionBase(this, UserControl.DataContextProperty).UpdateTarget();

But I get null as the result of GetBindingExpression(..) method and I'm wondering if I'm using this wrong.

Also, is there any other good way to refresh all bindings on the control (which use DataContext as the source).


回答1:


Well, you could just re-assign the DataContext:

var dataContext = DataContext;
DataContext = null;
DataContext = dataContext;

FYI, resetting the property to its value (i.e.DataContext = DataContext) won't work.




回答2:


You're using the BindingOperations.GetBindingExpressionBase method on the wrong property. You have to use it on the properties which are binding to the DataContext property, not the DataContext property itself.



来源:https://stackoverflow.com/questions/794370/update-all-bindings-in-usercontrol-at-once

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