c# - 如何为表单上的所有绑定(bind)调用 UpdateSource?

标签 c# .net wpf

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

最佳答案

前段时间我为这个任务写了一堆助手。

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

那你就打电话

this.UpdateAllBindingSources();
从你的窗口,你就完成了。

关于c# - 如何为表单上的所有绑定(bind)调用 UpdateSource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5150983/

相关文章:

c# - 需要 DotNetOpenAuth 教程

c# - 实例化 XmlDocument 很昂贵吗?

c# - WPF/XAML 绑定(bind) : Work with real DataContext

wpf - RadioButton 上的 DataTrigger IsChecked

c# - 如何确定命名文件夹的完整路径?

c# - 序列化和恢复未知类

c# - Linq to Sql - 具有多个连接的不同项目的查询列表

c# - 对两个List<>的操作

c# - 反序列化xml时忽略未知类型

wpf - Observable Collection 不更新 UI