SubzeroSC February 2016

How to get the type that a DependencyProperty uses for its value in Silverlight?

I want to check what the type of a DependencyProperty is and in WPF I could do the following:

DependencyProperty property = ...;
var typeAsString = property.PropertyType.Name;

As the PropertyType is only available in WPF.

I am wondering if there is another way of achieving this in Silverlight.

Answers


silverfighter February 2016

I think this might be worth a look for you. How to get a DependencyProperty by name in Silverlight?

You have to use reflection:

     public static DependencyProperty GetDependencyProperty(Type type, string name)
 {
     FieldInfo fieldInfo = type.GetField(name, BindingFlags.Public | BindingFlags.Static);
     return (fieldInfo != null) ? (DependencyProperty)fieldInfo.GetValue(null) : null;
 }

as seen in the answer the usage is:

 var dp = GetDependencyProperty(typeof(TextBox), "TextProperty");

HTH

Post Status

Asked in February 2016
Viewed 2,291 times
Voted 14
Answered 1 times

Search




Leave an answer