开发者

DependencyProperty from string

开发者 https://www.devze.com 2023-03-11 06:59 出处:网络
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 va

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.


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);

0

精彩评论

暂无评论...
验证码 换一张
取 消