Search code examples
.netreflectionwindows-runtimeattached-properties

How to get all Attached Properties that are set to a DependencyObject in WinRT?


What I want is to retrieve all the attached properties that are set to an instance of a Depedency Object.

For instance. If I have a <Button x:Name="myButton" Grid.Row="2"> later in the code, I want to be able to retrieve a list of the Attached Properties that has been set to the instance. Something like

List<DependencyProperty> attachedProperties = GetAttachedProperties(myButton);

Should return a List with the Grid.RowProperty Attached Property, since that it has been explicitly set to the instance of myButton.


Solution

  • The way I am doing it in my visual tree debugger tool is I scan the known assemblies in the package for all types and check for static properties of type DependencyProperty, then use a few heuristics to determine whether the dependency properties are attached - I check if there is a regular CLR property name-mapped to the dependency property (if there isn't - it usually indicates an attached property). Otherwise - if there is a Get method - it usually indicates an attached property. Once you have a list of all properties - you can call ReadLocalValue on the DependencyObject and if you get DependencyProperty.UnsetValue as a result - the property is not set on the object.

    If anyone has a better method for doing it - I would really like to hear it since it would be great if I can simplify my code.

    Oh and by the way - that's not a code I would ship in a store app. I use it for diagnostics/debugging only.