如何为窗体上的所有绑定调用UpdateSource? [英] How to invoke UpdateSource for all bindings on the form?

查看:28
本文介绍了如何为窗体上的所有绑定调用UpdateSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为表单上的所有绑定调用UpdateSource?

How to invoke UpdateSource for all bindings on the form?

推荐答案

前一段时间,我为此任务写了一堆助手.

Some time ago I wrote a bunch of helpers for this task.

public static void UpdateAllBindingSources(this DependencyObject obj)
{
    foreach (var binding in obj.GetAllBindings())
        binding.UpdateSource();
}

public static IEnumerable<BindingExpression> GetAllBindings(this DependencyObject obj)
{
    var stack = new Stack<DependencyObject>();

    stack.Push(obj);

    while (stack.Count > 0)
    {
        var cur = stack.Pop();
        var lve = cur.GetLocalValueEnumerator();

        while (lve.MoveNext())
            if (BindingOperations.IsDataBound(cur, lve.Current.Property))
                yield return lve.Current.Value as BindingExpression;

        int count = VisualTreeHelper.GetChildrenCount(cur);
        for (int i = 0; i < count; ++i)
        {
            var child = VisualTreeHelper.GetChild(cur, i);
            if (child is FrameworkElement)
                stack.Push(child);
        }
    }
}

然后,您只需从窗口中调用

Then you just call

this.UpdateAllBindingSources();

就可以了.

这篇关于如何为窗体上的所有绑定调用UpdateSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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