有没有办法让一个窗口中的所有BindingEx pression对象? [英] Is there a way to get all BindingExpression objects for a Window?
问题描述
有没有办法让一个窗口中的所有BindingEx pression对象?
Is there a way to get all BindingExpression objects for a Window?
我试图刷新表格时需要的PropertyChanged号事件被触发刷新的形式太高,而不是一个很好的选择。我想这样做表单/窗口可以重新查询的其他方式的所有绑定。
I am trying to refresh the form when the number PropertyChanged events that need to be fired to refresh a form is too high and not a good option. I am thinking doing it the other way that the form/window can re-query all bindings.
推荐答案
如果你筹集的PropertyChanged
与 PropertyChangedEventArgs
有一个参数空
或的String.Empty
的所有属性的绑定将更新。
If you raise PropertyChanged
with the PropertyChangedEventArgs
that have a parameter of null
or String.Empty
the bindings of all properties will update.
[<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.propertychangedeventargs.propertyname.aspx\"相对=nofollow> MSDN参考]
做它周围的其他方法是很多更复杂,可能更多的表现消费,我认为。您将需要检查每一个DependencyObject的每一个的DependencyProperty在绑定整个窗口。
Doing it the other way around is a lot more complicated and probably more performance consuming i think. You would need to check every DependencyProperty of every DependencyObject in the whole window for bindings.
编辑:写就你问的这是什么下面粗略扩展方法,这是非常低效的(有可能是改善的空间,但你仍然处理相当复杂的算法):
Wrote the following sketchy extension method which does what you asked for, it's awfully inefficient (there is probably room for improvement but you're still dealing with an algorithm of considerable complexity):
public static void UpdateAllBindings(this DependencyObject o)
{
//Immediate Properties
List<FieldInfo> propertiesAll = new List<FieldInfo>();
Type currentLevel = o.GetType();
while (currentLevel != typeof(object))
{
propertiesAll.AddRange(currentLevel.GetFields());
currentLevel = currentLevel.BaseType;
}
var propertiesDp = propertiesAll.Where(x => x.FieldType == typeof(DependencyProperty));
foreach (var property in propertiesDp)
{
BindingExpression ex = BindingOperations.GetBindingExpression(o, property.GetValue(o) as DependencyProperty);
if (ex != null)
{
ex.UpdateTarget();
}
}
//Children
int childrenCount = VisualTreeHelper.GetChildrenCount(o);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(o, i);
child.UpdateAllBindings();
}
}
这篇关于有没有办法让一个窗口中的所有BindingEx pression对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!