2

I need to determine if a control on my window has a specific dependency property declared. Here is an example of a button with the DP DemandRole. That DP can be declared for various control types, not only buttons. I'm trying to enumerate all controls on a window and return only those which have the DP DemandRole declared.

<Button x:Name="_reset"
        sec:SecurityAction.DemandRole="Admin,Engineer,SuperUser,Technician,Supervisor" 
        Content="_Reset"
        Visibility="Visible"
        Command="{Binding ResetPasswordCommand}" />

I've can get Dependency Properties for a specific type, but that only returns properties for the type and does contain the DP I have defined for the controls.

static IEnumerable<FieldInfo> GetDependencyProperties(Type type)
{
        var dependencyProperties = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)
                                       .Where(p => p.FieldType.Equals(typeof(DependencyProperty)));
        return dependencyProperties;
    }

Any idea how I can get all DPs on a specific instance of a control?

Thanks,

Lance

Lance
  • 411
  • 2
  • 6
  • 18

2 Answers2

2

I found this code example from the Getting list of all dependency/attached properties of an Object page on the Visual Studio Forum. I can't guarantee that it will work but it 'appears' to have helped the original question author.

public static class DependencyObjectHelper
{
    public static List<DependencyProperty> GetDependencyProperties(Object element)
    {
        List<DependencyProperty> properties = new List<DependencyProperty>();
        MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
        if (markupObject != null)
        {
            foreach (MarkupProperty mp in markupObject.Properties)
            {
                if (mp.DependencyProperty != null)
                {
                    properties.Add(mp.DependencyProperty);
                }
            }
        }

        return properties;
    }

    public static List<DependencyProperty> GetAttachedProperties(Object element)
    {
        List<DependencyProperty> attachedProperties = new List<DependencyProperty>();
        MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
        if (markupObject != null)
        {
            foreach (MarkupProperty mp in markupObject.Properties)
            {
                if (mp.IsAttached)
                {
                    attachedProperties.Add(mp.DependencyProperty);
                }
            }
        }

        return attachedProperties;
    }
}

If these extension methods don't help, there are other examples on the linked page.


UPDATE >>>

I just found this question here on Stack Overflow that might also help:

How to enumerate all dependency properties of control?

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
1

Not tested, but I guess you need BindingFlags.FlattenHierarchy flag.

static IEnumerable<FieldInfo> GetDependencyProperties(Type type)
{
     var dependencyProperties = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy)
                                       .Where(p => p.FieldType.Equals(typeof(DependencyProperty)));
     return dependencyProperties;
}

If that doesn't work, you need to call type.BaseType.GetFields recursively till BaseType returns null and concat all the fields.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • FlattenHierachy didn't return the DP, recursively going through the base types also didn't return the DP. – Lance Dec 22 '14 at 14:18