0

我在 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...
        }                       
    }
4

1 回答 1

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()给他们。

于 2013-10-23T18:15:28.137 回答