【问题标题】:Iterate over VisualTreeHelper.GetChild() and call UpdateSource on Controls that contain databinding遍历 VisualTreeHelper.GetChild() 并在包含数据绑定的控件上调用 UpdateSource
【发布时间】:2013-10-23 17:52:26
【问题描述】:

我在 WPF 中有一个 ContentControl,其中包含一些输入控件,例如 TextBoxes 和 ComboBoxes。 这些控件中的每一个都通过UpdateSourceTrigger=Explicit 数据绑定到 ViewModel 中的给定属性。

当我点击一些“提交”按钮时,我想遍历FormularioPaciente 的每个具有绑定的子级,并调用UpdateSource

    private void btnSalvarEditarPaciente_Click(object sender, System.Windows.RoutedEventArgs e) {
        foreach (var childControl in LogicalTreeHelper.GetChildren(FormularioPaciente)) {
            // what should I do now?
            // I would really like to "auto-find" everything that should be updated...
        }                       
    }

【问题讨论】:

  • stackoverflow.com/questions/3586870/…这个答案可以帮你找到所有的绑定
  • @nit 我认为它是完全重复的,但事实并非如此,因为它告诉了如何以BindingBase 查找绑定,但没有告诉如何从中调用BindingExpression.UpdateSource。 ..
  • 嗯..我已经为你更新了答案..希望对你有帮助

标签: wpf data-binding visualtreehelper updatesourcetrigger


【解决方案1】:

我认为您可以稍微更新一下解决方案

   void GetBindingsRecursive(DependencyObject dObj, List<BindingExpressions> bindingList)
        {
            bindingList.AddRange(DependencyObjectHelper.GetBindingObjects(dObj));

            int childrenCount = VisualTreeHelper.GetChildrenCount(dObj);
            if (childrenCount > 0)
            {
                for (int i = 0; i < childrenCount; i++)
                { 
                    DependencyObject child = VisualTreeHelper.GetChild(dObj, i);
                    GetBindingsRecursive(child, bindingList);
                }
            }
        }

 public static class DependencyObjectHelper
        {
            public static List<BindingExpression> GetBindingObjects(Object element)
            {
                List<BindingExpression> bindings = new List<BindingBase>();
                List<DependencyProperty> dpList = new List<DependencyProperty>();
                dpList.AddRange(GetDependencyProperties(element));
                dpList.AddRange(GetAttachedProperties(element));

                foreach (DependencyProperty dp in dpList)
                {
                    BindingExpression b = BindingOperations.GetBindingExpression(element as DependencyObject, dp);
                    if (b != null)
                    {
                        bindings.Add(b);
                    }
                }

                return bindings;
            }

            public static List<DependencyProperty> GetDependencyProperties(Object element)
            {
                List<DependencyProperty> properties = new List<DependencyProperty>();
                MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
                if (markupObject != null)
                {
                    foreach (MarkupProperty mp in markupObject.Properties)
                    {
                        if (mp.DependencyProperty != null)
                        {
                            properties.Add(mp.DependencyProperty);
                        }
                    }
                }

                return properties;
            }

            public static List<DependencyProperty> GetAttachedProperties(Object element)
            {
                List<DependencyProperty> attachedProperties = new List<DependencyProperty>();
                MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
                if (markupObject != null)
                {
                    foreach (MarkupProperty mp in markupObject.Properties)
                    {
                        if (mp.IsAttached)
                        {
                            attachedProperties.Add(mp.DependencyProperty);
                        }
                    }
                }

                return attachedProperties;
            }
        }

一旦您获得BindingExpression 列表,您就可以在这些列表上调用BindingExpression.UpdateSource()

【讨论】:

  • 好友为精彩帖子 +1!
  • @devhedgehog 谢谢我的朋友 :) 虽然我只是在 SO 上修改了现有答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 2016-08-19
  • 1970-01-01
  • 2020-01-03
相关资源
最近更新 更多