List DependencyProperties?

How can I get a list of DependencyProperties from an object



Answer this question

List DependencyProperties?

  • Jehan Badshah

    That's handy code, but I'll offer some caveats for it.

    * The typename that was chosen for the illustrated class has potential for name collision with another class DependencyPropertyHelper that is already defined in System.Windows: http://msdn2.microsoft.com/en-us/library/system.windows.dependencypropertyhelper.aspx

    Depending on how you do your references/includes etc., you might want to use a different class name if you wanted to add the code in this post to your application or library.

    * The way the GetProperties is written, it doesn't exclude the attached properties. Semantically, attached properties are dependency properties (at least the WPF ones are, by implementation). But syntactically, the usages are different in either XAML or code. So depending on what you're doing with the information, you might want to tweak GetProperties to exclude the APs that GetAttachedProperties returns.

    * The code will end up revealing a basic truth about attached properties in WPF: all attached properties exist on all DependencyObjects, so it doesn't particularly matter which Object element you pass in to GetAttachedProperties so long as it's a DO subtype. Result is you'll get dozens of APs listed on any possible DO, and barring inclusion of DLLs other than the WPF project defaults it'll always be the same list.


  • mbakhodir

    Perfect - Thanx :-D
  • Brandon Hawkins

    The following helper methods can do the trick:
    public static class DependencyPropertyHelper
        {
           public static IList<DependencyProperty> GetAttachedProperties(Object element)
           {
               List<DependencyProperty> attachedProperties = new List<DependencyProperty>();
     
               foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(element, 
                  new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.SetValues | 
                                                                                 PropertyFilterOptions.UnsetValues | 
                                                                                 PropertyFilterOptions.Valid) }))
               {
                  DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(pd);
                  if (dpd != null && dpd.IsAttached)
                  {
                      attachedProperties.Add(dpd.DependencyProperty);
                  }
               }
     
               return attachedProperties;
           }
     
           public static IList<DependencyProperty> GetProperties(Object element)
           {
               List<DependencyProperty> properties = new List<DependencyProperty>();
     
               foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(element,
                  new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.SetValues | 
                                                                                 PropertyFilterOptions.UnsetValues | 
                                                                                 PropertyFilterOptions.Valid) }))
               {
                  DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(pd);
                  if (dpd != null)
                  {
                      properties.Add(dpd.DependencyProperty);
                  }
               }
     
               return properties;
           }
        }

    Disclaimer: The original author of the above code is Douglas Stockwell.

    Sheva


  • List DependencyProperties?