1

我有以下代码:

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

PropertyName 是一个字符串,包含我要获取的属性的名称。这适用于“正常”属性,但我无法获得“Canvas.LeftProperty”或“Canvas.TopProperty”。

谁能帮我吗?

谢谢,克里斯

4

1 回答 1

1

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

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

来源

于 2012-07-18T07:16:40.970 回答