【发布时间】:2011-05-27 14:06:29
【问题描述】:
我正在尝试从通过 XML 文件读取的数据中设置 WPF 控件的属性(高度、宽度、字体粗细、边距和许多其他)。我不会知道要预先设置哪些属性。我想知道是否有人知道通过反射来做到这一点?
目前,我已经设法使用反射分配所有原始类型和枚举类型,但我在使用 FontWeight、Margin、Background 和许多其他需要其他属性的属性时遇到了一些问题例如,设置属性中的对象:要设置按钮的 FontWeight 属性,您必须这样做。
button.FontWeight = Fontweights.Bold;
或边距
button.Margin = new Thickness(10, 10, 10, 10);
由于可以在 WPF 中的控件上设置可能的 150 多个属性,我只是想避免这种代码。
public void setProperties(String propertyName, string PropertyValue
{
if(propertyName = "Margin")
{
Set the margin.....
}
else if (propertyName = "FontWeight")
{
set the FontWeight....
}
}
对于可以在 WPF 控件上设置的每个可能的属性,依此类推。
【问题讨论】:
-
这与您的其他question 有何不同?
标签: wpf reflection