Question

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.

Was it helpful?

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.

OTHER TIPS

Well, I found out that you just cannot query for all properties attached to a Dependency Object. Attached Properties are actually no part of the instance itself, but by the type that registers it.

For my problem, I initially wanted to retrieve all the attached properties set to a Dependency Object, but later discovered that what I really needed is to be able to get a Attached Property from a string like "Canvas.Left" and a Dependency Object (target). I just have to get the information from the string, query the declared properties from Canvas and find LeftProperty. Then, I can get or set its value using Target.GetValue(dp).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top