一次更新UserControl中的所有绑定 [英] Update all bindings in UserControl at once

查看:69
本文介绍了一次更新UserControl中的所有绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当UserControl的可见性更改为Visible时,我需要更新其所有绑定。我几乎所有的绑定都绑定到用​​户控件的DataContext属性,因此我试图更新该绑定的目标:

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();

但是我得到 null 的结果 GetBindingExpression(..)方法,我想知道我是否使用了这个错误。

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

另外,是还有其他方法可以刷新控件上的所有绑定(使用DataContext作为源)。

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

推荐答案

好吧,您可以只是重新分配 DataContext

Well, you could just re-assign the DataContext:

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

仅供参考,将属性重置为其值(即 DataContext = DataContext )无效。

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

这篇关于一次更新UserControl中的所有绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆