14

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

5 Answers 5

14

Using reflection is not needed (and a bad idead IMHO) because the framework has already utility classes for this (but they're not obvious to find :-).

Here is an answer based on this article: Enumerate Bindings and the LocalValueEnumerator Structure

    public static IEnumerable<DependencyProperty> EnumerateDependencyProperties(this DependencyObject obj)
    {
        if (obj != null)
        {
            LocalValueEnumerator lve = obj.GetLocalValueEnumerator();
            while (lve.MoveNext())
            {
                yield return lve.Current.Property;
            }
        }
    }

Here is another answer based on this other article: Getting list of all dependency/attached properties of an Object that uses MarkupWriter.GetMarkupObjectFor Method.

    public static IEnumerable<DependencyProperty> EnumerateDependencyProperties(object element)
    {
        if (element != null)
        {
            MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
            if (markupObject != null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    if (mp.DependencyProperty != null)
                        yield return mp.DependencyProperty;
                }
            }
        }
    }

    public static IEnumerable<DependencyProperty> EnumerateAttachedProperties(object element)
    {
        if (element != null)
        {
            MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
            if (markupObject != null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    if (mp.IsAttached)
                        yield return mp.DependencyProperty;
                }
            }
        }
    }
13
public IList<DependencyProperty> GetAttachedProperties(DependencyObject obj)
{
    List<DependencyProperty> result = new List<DependencyProperty>();

    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

0
5

You can use reflection, via the GetFields, method to find all the public static properties on TextBox. You can then use a Linq Where clause to filter these to any of type DependencyProperty:

  var flags = BindingFlags.Static |
              BindingFlags.FlattenHierarchy |
              BindingFlags.Public;
  var dependencyProperties = typeof(TextBox).GetFields(flags)
                     .Where(f => f.FieldType == typeof(DependencyProperty));

You can then transform this to a list of names via a Select:

  var dependencyProperties = typeof(TextBox).GetFields(flags)
                     .Where(f => f.FieldType == typeof(DependencyProperty))
                     .Select(dp => dp.Name);

Note: each name has a 'Property' suffix, you can of course remove this in the above Select clause if you like.

4
  • +1 for BindingFlags.FlattenHierarchy. Mine does not have it.
    – Aliostad
    Jan 25, 2011 at 13:45
  • This solution isn't good. See this topic at MSDN: social.msdn.microsoft.com/Forums/en-US/wpf/thread/… . See Dr.WPF's comment.
    – Dennis
    Jan 25, 2011 at 13:46
  • Regarding Dr.WPFs comments, (1) Sure, reflection is slow, but it is the only way to do this, if speed is an issue, cache the results. (2) The method above does not rely on the presence of CLR accessors, so this is not an issue, (3) This method will find attached properties also.
    – ColinE
    Jan 25, 2011 at 13:54
  • Colin, create a static class, register an dependency property as attached property in that class, and bind to your attached property to any control. Then enumerate control's dependency properties via your code.
    – Dennis
    Jan 25, 2011 at 14:48
1

If you want name of the DependencyProperties of an element then you can do this:

    var results = from prop in typeof(element).GetFields() 
                  where prop.FieldType == typeof(DependencyProperty)
                  select prop.Name.Substring(0, prop.Name.Length - 8);

where 8 is length of the string "Property" which appears at the end of a dependency property!

0

Try

        var fieldInfos = typeof(TextBox).GetFields( BindingFlags.Public | BindingFlags.Static).Where(x=>x.FieldType == typeof(DependencyProperty));
        foreach (var fieldInfo in fieldInfos)
        {
            Console.WriteLine(fieldInfo.Name);
        }

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.