Retrieve all Data Bindings from WPF Window

前端 未结 2 773
慢半拍i
慢半拍i 2020-12-12 01:26

I have a WPF form which has many controls on it. Many (but not all) of these controls are databound to an underlying object. At certain times, such as when the Save button

相关标签:
2条回答
  • 2020-12-12 02:08

    There is a better solution in .NET 4.5 and above:

    foreach (BindingExpressionBase be in BindingOperations.GetSourceUpdatingBindings(element))
    {
        be.UpdateSource();
    }
    
    0 讨论(0)
  • 2020-12-12 02:13

    Try out my sample below. I haven't fully tested this so it may have issues. Also, performance may be questionable. Maybe others can help out to make it faster. But anyway, it seems to do the trick.

    Note: A limitation to this, however, is that it may not pick up the bindings defined within Styles or DataTemplates. I'm not sure though. Needs more testing.

    Anyway, the solution has three parts basically:

    1. Use VisualTreeHelper to walk the entire visual tree.
    2. For each item in the visual tree, get all dependency properties. Reference.
    3. Use BindingOperations.GetBindingBase to get the binding for each property.

    GetBindingsRecursive function:

    void GetBindingsRecursive(DependencyObject dObj, List<BindingBase> 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);
                    }
                }
            }
    

    DependencyObjectHelper class:

    public static class DependencyObjectHelper
        {
            public static List<BindingBase> GetBindingObjects(Object element)
            {
                List<BindingBase> bindings = new List<BindingBase>();
                List<DependencyProperty> dpList = new List<DependencyProperty>();
                dpList.AddRange(GetDependencyProperties(element));
                dpList.AddRange(GetAttachedProperties(element));
    
                foreach (DependencyProperty dp in dpList)
                {
                    BindingBase b = BindingOperations.GetBindingBase(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;
            }
        }
    

    Sample usage:

    List<BindingBase> bindingList = new List<BindingBase>();
    GetBindingsRecursive(this, bindingList);
    
    foreach (BindingBase b in bindingList)
    {
         Console.WriteLine(b.ToString());
    }
    
    0 讨论(0)
提交回复
热议问题