WPF。发现,结合具体的房地产调控 [英] WPF. Find control that binds to specific property

查看:126
本文介绍了WPF。发现,结合具体的房地产调控的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是如何实现给定一个PROPERTYNAME的方法,任何想法找到一个控制(可能是从一个的VisualTree)这势必给定属性?

Any ideas on how to implement a method that given a propertyname, finds a control (perhaps from a visualtree) which is bound to the given property?

推荐答案

试试这个。首先,复制粘贴此 DependencyObjectHelper 在您的项目类。它有一个功能,它可以让所有的BindingObjects在一个给定的对象。

Try this one. First, copy-paste this DependencyObjectHelper class in your project. It has a function that allows you to get all the BindingObjects in a given object.

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

然后,创建这个 GetBindingSourcesRecursive 功能。递归收集在具有至少一个结合对象打靶给定属性的名称。可视树的DependencyObjects

Then, create this GetBindingSourcesRecursive function. It recursively collects the DependencyObjects in the visual tree that has at least one Binding object targetting a given property name.

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

然后,利用这一点,只需调用 GetBindingsRecursive 传递属性名,根视觉(例如窗口)和对象列表将包含结果。

Then, to use this, just call GetBindingsRecursive passing in the property name, the root visual (e.g. the Window), and an object list that will contain the results.

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

希望这有助于。

这篇关于WPF。发现,结合具体的房地产调控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆