WPF.查找绑定到特定属性的控件

fsl*_*fsl 6 data-binding wpf binding

关于如何实现给定属性名的方法的任何想法,找到绑定到给定属性的控件(可能来自visualtree)?

ASa*_*nch 11

试试这个吧.首先,在项目中复制粘贴此DependencyObjectHelper类.它有一个函数,允许您获取给定对象中的所有BindingObjects.

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(DependencyObjectHelper.GetDependencyProperties(element));
        dpList.AddRange(DependencyObjectHelper.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,创建此GetBindingSourcesRecursive函数.它以可视树形式递归收集DependencyObject,其中至少有一个Binding对象以给定的属性名称为目标.

private void GetBindingSourcesRecursive(string propertyName, DependencyObject root, List<object> sources)
{
    List<BindingBase> bindings = DependencyObjectHelper.GetBindingObjects(root);
    Predicate<Binding> condition =
        (b) =>
        {
            return (b.Path is PropertyPath) 
                && (((PropertyPath)b.Path).Path == propertyName)
                && (!sources.Contains(root));
        };

    foreach (BindingBase bindingBase in bindings)
    {
        if (bindingBase is Binding)
        {
            if (condition(bindingBase as Binding))
                sources.Add(root);
        }
        else if (bindingBase is MultiBinding)
        {
            MultiBinding mb = bindingBase as MultiBinding;
            foreach (Binding b in mb.Bindings)
            {
                if (condition(bindingBase as Binding))
                    sources.Add(root);
            }
        }
        else if (bindingBase is PriorityBinding)
        {
            PriorityBinding pb = bindingBase as PriorityBinding;
            foreach (Binding b in pb.Bindings)
            {
                if (condition(bindingBase as Binding))
                    sources.Add(root);
            }
        }
    }

    int childrenCount = VisualTreeHelper.GetChildrenCount(root);
    if (childrenCount > 0)
    {
        for (int i = 0; i < childrenCount; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(root, i);
            GetBindingSourcesRecursive(propertyName, child, sources);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,要使用它,只需调用GetBindingsRecursive传递属性名称,根视觉(例如Window)和包含结果的对象列表.

List<object> sources = new List<object>();
GetBindingSourcesRecursive("SomePropertyPath", this, sources);
sources.ForEach((o) => Console.WriteLine(o.ToString()));
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.