如何在 PropertyGrid 中查看对象属性?
目前我有一个 A 类型的对象,PropertyGrid 正在查看它。然而,它的属性之一是 B 类型。B 类型的属性是不可扩展的。我怎样才能改变这个,以便:
a)我可以扩展自定义对象属性 b)这些更改绑定到该属性
这是我到目前为止的代码:
using System;
using System.Windows.Forms;
using System.ComponentModel;
namespace PropGridTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
A a = new A
{
Foo = "WOO HOO!",
Bar = 10,
BooFar = new B
{
FooBar = "HOO WOO!",
BarFoo = 100
}
};
propertyGrid1.SelectedObject = a;
}
}
public class A
{
public string Foo { get; set; }
public int Bar { get; set; }
public B BooFar { get; set; }
}
public class B
{
public string FooBar { get; set; }
public int BarFoo { get; set; }
}
}
At the moment I have an object of type A which is being viewed by the PropertyGrid. However, one of its properties is of type B. The property which is of type B is not expandable. How can I change this so that:
a) I can expand custom object property's
b) Those changes are bound to that property
Here is the code I have so far:
using System;
using System.Windows.Forms;
using System.ComponentModel;
namespace PropGridTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
A a = new A
{
Foo = "WOO HOO!",
Bar = 10,
BooFar = new B
{
FooBar = "HOO WOO!",
BarFoo = 100
}
};
propertyGrid1.SelectedObject = a;
}
}
public class A
{
public string Foo { get; set; }
public int Bar { get; set; }
public B BooFar { get; set; }
}
public class B
{
public string FooBar { get; set; }
public int BarFoo { get; set; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
ExpandableObjectConverter
为此目的而设计的类。要使用此转换器,请使用 修饰相关属性
TypeConverterAttribute
,以typeof(ExpandableObjectConverter)
作为构造函数参数。You can use the
ExpandableObjectConverter
class for this purpose.To use this converter, decorate the property in question with the
TypeConverterAttribute
, withtypeof(ExpandableObjectConverter)
as the constructor-argument.