Getting the attached property “Canvas.Left”

2019-02-27 22:55发布

I have the following code:

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

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".

Can anyone help me out?

Thanks, Chris

1条回答
手持菜刀,她持情操
2楼-- · 2019-02-27 22:57

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

Source

查看更多
登录 后发表回答