迭代VisualTreeHelper.GetChild()并在包含数据绑定的控件上调用UpdateSource

时间:2013-10-23 17:52:26

标签: wpf data-binding visualtreehelper updatesourcetrigger

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

当我点击一些“提交”按钮时,我想遍历每个有{绑定的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...
        }                       
    }

1 个答案:

答案 0 :(得分:2)

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

   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()