【发布时间】:2009-05-25 11:43:03
【问题描述】:
我正在根据 EAV 类型架构中保存的一组元数据动态创建捕获表单。
我的麻烦在于将数据加载回控件,尤其是 winforms 组合框。
对于绑定到控件的数据也使用实体框架。
检查是否存在控件,否则创建。为每个映射属性设置它们的值。 即数据源、DisplayMember、ValueMember 等...
加载值是否存在于 SelectedValue 属性中?这是哪里失败了?
在检查对象时,似乎还没有加载包括数据源在内的先前值?但是组合框确实显示了渲染后的值吗?
这里是一些sn-ps的代码。
Type oType = Type.GetType("System.Windows.Forms.ComboBox");
if (oControlObject == null)
{
oControlObject = (Control)Activator.CreateInstance(oType);
oControlObject.Tag = item;
oControlObject.CreateControl();
}
...Loop to set Datasource, DisplayMember & ValueMember ...
if (property.IsReadProperty.Value && value != null)
{
PropertyInfo propSet = oType.GetProperty(property.PropertyName); //PropertyName here is "SelectedValue"
propSet.SetValue(oControlObject, value.Value, null);
}
【问题讨论】:
-
好吧,如果我声称我理解你的意思,那我就是在撒谎,但是,投射到 ComboBox 而不是控制呢?我不知道,我可能完全错了。
-
此时我正在使用反射与对象进行交互,因此投射不应该有所作为。但是很好的建议。无论如何都会尝试,永远不知道?
标签: c# winforms reflection