开发者

How to invoke UpdateSource for all bindings on the form?

开发者 https://www.devze.com 2023-02-13 21:19 出处:网络
How to invoke UpdateSo开发者_StackOverflowurce for all bindings on the form?Some time ago I wrote a bunch of helpers for this task.

How to invoke UpdateSo开发者_StackOverflowurce 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();
from your window and you are done.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号