在 PropertyGrid 上显示对象动态类型的属性
我需要启用任意对象的编辑属性(对象的类型仅在运行时已知)。我创建了以下类:
public class Camera
{
[TypeConverter(typeof(ExpandableObjectConverter))]
public object Configuration
{
get
{
return configuration;
}
set
{
configuration = value;
}
}
public Class1 a;
[TypeConverter(typeof(ExpandableObjectConverter))]
public Class1 A
{
get
{
return a;
}
set
{
a = value;
}
}
}
选择对象“Camera”后,我可以在PropertyGrid上看到Class1的属性,但看不到对象“Configuration”的属性。我该如何解决这个问题?
I need to enable editing properties of arbitrary objects (the type of object is only known at run-time). I created the following class:
public class Camera
{
[TypeConverter(typeof(ExpandableObjectConverter))]
public object Configuration
{
get
{
return configuration;
}
set
{
configuration = value;
}
}
public Class1 a;
[TypeConverter(typeof(ExpandableObjectConverter))]
public Class1 A
{
get
{
return a;
}
set
{
a = value;
}
}
}
After selecting object "Camera", I can see the property of Class1 on PropertyGrid, but I can't see the property of object "Configuration". How can I fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的假设是您的表单在分配配置属性之前变得可见。您没有提供足够的代码来查看情况是否如此。为了测试我的担忧,我创建了两个配置对象:
并将
您的相机类修改为如下所示:
然后我创建了一个带有 PropertyGrid 和两个 Button 实例的表单。我像这样配置了表单交互:
启动视图如下所示:
单击第一个按钮后:
< img src="https://i.sstatic.net/ZNMfv.jpg" alt="在此处输入图像描述">
单击第二个按钮后:
如果删除刷新,属性网格将无法正常工作。另一种方法是在类和属性上提供带有 INotifyPropertyChanged 的接口。
My assumption was that your form becomes visible before the Configuration property was assigned. You didn't supply enough code to see if that was the case. In order to test out my concern, I created two configuration objects:
and
I modified your camera class to look like this:
I then created a form with a PropertyGrid and two Button instances. I configured the form interactions like this:
The startup view looks like this:
After clicking the first button:
After clicking the second button:
If you remove the refresh, the property grid does not work correctly. The alternative is to supply an interface with INotifyPropertyChanged on your classes and properties.