In Visual Studio, in
the XAML view when we add a element tag say <Rectangle...> and
hit the spacebar, we get a list of all the attached properties and
dependency properties of the object. (For e.g. Canvas.Left).
How
can this be achieved through procedural code I tried using Reflection
(on a Rectangle object in above case) but it did not help.
Thanks,
Niranjan

Getting list of all dependency/attached properties of an Object
R Raghu
Sheva
Dorisdvu
Joshizzle
you can try something like this
Type t = typeof(Button); while (t != null){
FieldInfo[] fs = t.GetFields();System.Diagnostics.
Debug.WriteLine(t.ToString()); for (int i = 0; i < fs.Length; i++)System.Diagnostics.
Debug.WriteLine("\t" + fst = t.BaseType;
}
mr4100
this might be better
Type
t = typeof(Rectangle); while (t != null){
FieldInfo[] fi = t.GetFields(); for(int i=0;i<fi.Length;i++) if (fiSystem.Diagnostics.
Debug.WriteLine(fit = t.BaseType;
}
venp
we dont get attached properties, if we do like I mentioned
I think Doug's way is better
Steveinbeloit
{{{{{properties.Add(mp.DependencyProperty);}}}}{{{{attachedProperties.Add(mp.DependencyProperty);}}}}}Sheva
RhysDavies
Thanks for your great inputs. I have it working now. Initially I used a combination of Doug's and Lee's code and got it to work. Thougg Zhou's code is more specific to the requirement and works too.
I had not come across MarkupObject and MarkupWriter classes. Another observation I made was that for these classes to work, the element has to be a part of a logical tree. So if I try to create a new Rectangle object and use it , markupobject.Properties is empty. On the other hand if the rectangle is a part of the logical tree, the relevant properties are returned.
The documentation says that these classes are primarily used to serialize logical trees into XAML format which seems somewhat in harmony to the above observation.
Any ideas on this
Thanks,
Niranjan.
AaronL
donbox5
You can obtain attached properties using reflection. Here's an example that iterates through all the types in an assembly:
Note the use of DependencyPropertyDescriptor to obtain additional information about each Dependency Property (this allows us to check if they are Attached or not). There may be a better method that avoids iterating over all the types like this, but I haven't found it yet!
- Doug
Buddhist
Actually I thought I found a slightly more elegant solution:
But, it would appear this will not give any DP's which we define.
- Doug
Trisk2
Zhou ,
your code will only get the properties that are set on the element, not all the properties available for the element. Am I missing something
dav3333333
Sheva
GreenLeyland
Thanks for the response. Here is my specific requirement -
In my logical tree, I want the user to be able to provide any object name and any property name on that object as string inputs. Based on these inputs, I need to set a value on that property for that object (more specifically I want to set a binding on that property).
Thus, for instance if I had a Rectangle object called "myRect" in the logical tree. The user inputs would be:
Element Name : myRect
Element Property : Canvas.Left
Path : SomeInput (used for databinding)
I then need to get a Dependency property object corresponding to Canvas.Left and set the binding on myRect object programatically (I am using the FrameworkElement.SetBinding method).
The problem is , as you already mentioned, Canvas.Left has no relationship to Rectangle so I cannot look into Rectangle properties. And moreover the user can provide any property name that can participate in Data Binding. How can I get the corresponding dependency property object in this case
Thanks,
Niranjan.
Raoul_BennetH
>{{{InitializeComponent();}{myTextBlock.Text = GetAllDPs(btn);}{{{{sb.AppendLine(property.Name);}}}}}{{}{element.SetValue(BarAttachedProperty, value);}}}Please take a closer look at the code hightlighted in red colour, actually my code also supports custom attched DP scenario.
Sheva