2013-10-23 29 views
0

我在WPF中有一個ContentControl,它包含一些輸入控件,如TextBoxes和ComboBoxes。 這些控件中的每一個都是數據綁定到ViewModel中的給定屬性,其中UpdateSourceTrigger=Explicit迭代VisualTreeHelper.GetChild()並在包含數據綁定的控件上調用UpdateSource

當我點擊了一些「提交」按鈕,我要穿越的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... 
     }      
    } 
+0

http://stackoverflow.com/questions/3586870/retrieve-all-data-bindings-from-wpf-window/3587263#3587263這個答案可以幫助你找到所有的綁定 – Nitin

+0

@nit我認爲這是一個確切的重複,但IT不是,因爲它告訴如何找到綁定作爲'BindingBase',但不告訴如何調用'BindingExpression.UpdateSource' ... – heltonbiker

+0

嗯..我已經更新了你的答案。 。希望它可以幫助 – Nitin

回答

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()那些。

+0

好友發佈好友+1! –

+0

@devhedgehog謝謝你,我的朋友:)雖然我只是在SO上推送了現有的答案 – Nitin

相關問題