Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DependencyProperty from string

How do I convert a property name (in string) to a DependencyProperty?

I have a set of property names, its values in string and a DependencyObject. Now I want to set these property values to the DependencyObject. Any idea on how this can be achieved?

Thanks.

like image 396
sudarsanyes Avatar asked Jun 09 '11 10:06

sudarsanyes


1 Answers

You can get DependencyPropertyDescriptor using DependencyPropertyDescriptor.FromName method and then get dependency property identifier from this descriptor.


var descriptor = DependencyPropertyDescriptor.FromName(
    propertyName,
    dependencyObject.GetType(),
    dependencyObject.GetType());

// now you can set property value with
descriptor.SetValue(dependencyObject, value);

// also, you can use the dependency property itself
var property = descriptor.DependencyProperty;
dependencyObject.SetValue(property, value);

like image 53
Konstantin Oznobihin Avatar answered Oct 14 '22 10:10

Konstantin Oznobihin