How to enumerate all dependency properties of control?

前端 未结 5 1802
名媛妹妹
名媛妹妹 2020-12-01 21:17

I have some WPF control. For example, TextBox. How to enumerate all dependency properties of that control (like XAML editor does)?

5条回答
  •  醉话见心
    2020-12-01 21:58

    public IList GetAttachedProperties(DependencyObject obj)
    {
        List result = new List();
    
        foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(obj,
            new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) }))
        {
            DependencyPropertyDescriptor dpd =
                DependencyPropertyDescriptor.FromProperty(pd);
    
            if (dpd != null)
            {
                result.Add(dpd.DependencyProperty);
            }
        }
    
        return result;
    }
    

    Found here: http://social.msdn.microsoft.com/Forums/en/wpf/thread/580234cb-e870-4af1-9a91-3e3ba118c89c

提交回复
热议问题