0

I have a ContentControl in WPF which contains some input Controls, like TextBoxes and ComboBoxes. Each of these Controls is databound to a given property in the ViewModel, with UpdateSourceTrigger=Explicit.

When I click some "Submit" button, I want to traverse every Child of FormularioPaciente that has bindings, and call 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...
        }                       
    }
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • http://stackoverflow.com/questions/3586870/retrieve-all-data-bindings-from-wpf-window/3587263#3587263 this answer can help you find all the bindings – Nitin Oct 23 '13 at 17:55
  • @nit I thought it was an exact duplicate, but IT ISN'T, since it tells how to FIND the bindings as `BindingBase`, but does not tell how to call `BindingExpression.UpdateSource` from that... – heltonbiker Oct 23 '13 at 18:05
  • hmm..i have updated the answer for you.. hope it helps – Nitin Oct 23 '13 at 18:28

1 Answers1

2

I think you can update the solution a bit

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

and once you get the BindingExpression list you can call BindingExpression.UpdateSource() on those.

Nitin
  • 18,344
  • 2
  • 36
  • 53