如何使用字符串值作为这些属性的名称来分配对象属性 [英] How to assign object properties using string values as names of those properties

查看:154
本文介绍了如何使用字符串值作为这些属性的名称来分配对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我的wpf窗口上有一个名为tb1的文本框。



加载时我想为tb1设置一些属性我们背景和FontSize。

通常它看起来像这样:

Lets assume that I have wpf window with a textbox named "tb1" on it.

On loading I would like to set some properties for "tb1" such us "Background" and "FontSize".
Normally it would look something like this:

tb1.Background = Brushes.Red
tb1.FontSize = 10





但是现在我有一个任务要读取并设置所有SQL数据库中tb1的属性。所以我有一个带有列(nvarchar)和值的表:



ObjName; ObjProp; ObjValue

tb1;背景;红色

tb1;字体大小; 10



我将这个值重写为DataTable。我无法弄清楚的问题是 - 如何使用这些字符串值作为对象属性名来设置对象的属性值?



But now I have a task to read and set all the properties for tb1 from SQL database. So I have a table with columns(nvarchar) and values:

ObjName; ObjProp; ObjValue
tb1; Background; Red
tb1; FontSize; 10

I have red this values into DataTable. And the question I can't figure out is - how can I use those string values as object property names to set property values on object?

推荐答案

使用.FindName( )在可视化树中定位控件;然后使用反射(即PropertyInfo)来定位特定属性并获取/设置它们的值。
Use .FindName() to locate controls in the visual tree; then use reflection (i.e. PropertyInfo) to locate particular properties and get / set their values.


这篇关于如何使用字符串值作为这些属性的名称来分配对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆