获得附加属性"Canvas.Left". [英] Getting the attached property "Canvas.Left"

查看:133
本文介绍了获得附加属性"Canvas.Left".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

this.Object.GetType().GetProperty(this.PropertyName).GetValue(this.Object, null);

PropertyName是一个字符串,包含要获取的属性的名称.对于常规"属性,此方法工作正常,但我无法获取"Canvas.LeftProperty"或"Canvas.TopProperty".

PropertyName is a string, containing the name of the property I want to get. This works fine for "normal" properties, but I can't get the "Canvas.LeftProperty" or "Canvas.TopProperty".

有人可以帮我吗?

谢谢, 克里斯

推荐答案

我认为这是因为Canvas.Left是附加属性,要检索它们,请尝试以下操作:

I think this is because Canvas.Left is attached property and to retrieve them try this:

private DependencyProperty GetAttachedProperty(DependencyObject obj, string propertyName, Type ownerType)
{

    foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(obj,
        new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) }))
    {
        DependencyPropertyDescriptor dpd =
            DependencyPropertyDescriptor.FromProperty(pd);

        if (dpd != null && dpd.IsAttached)
        {
            if (string.Compare(dpd.DependencyProperty.Name, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0 && dpd.DependencyProperty.OwnerType == ownerType)
            {
                return dpd.DependencyProperty;
            }
        }
    }

    return null;
}

来源

这篇关于获得附加属性"Canvas.Left".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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