在 PropertyGrid 中显示对象描述
我希望在用户选择它们时在属性网格中显示许多对象。我知道可以在每个对象类中设置属性描述,但是我要求同一对象的不同实例之间的描述不同。
有没有一种方法可以在运行时设置整个对象的描述,无论在属性网格中选择什么属性,该描述都会显示?
例如,如果我有以下类
public class Person
{
public String Name { get; set; }
public String Age { get; set; }
public Person(String n, int a)
{
this.Name = n;
this.age = a;
}
public Person()
{
}
}
,并且按以下方式创建了一个 Person 对象
Person Frank = new Person(Frank, 22);
,并在属性网格中显示该对象,那么
propertyGrid1.SelectedObject = Frank;
我希望能够提供整个对象的描述,而不是提供对象的姓名和年龄属性Person 类。而且,因为我希望描述特别与 Frank 对象相关,所以我希望能够不仅根据选择的对象类型,而且根据该对象的特定实例来设置此描述。这可能吗?
I a number of objects that I would like to display within a property grid as they are selected by the user. I am aware that property descriptions can be set within each objects class, however I require that the descriptions differ between different instances of the same object.
Is there a way I can set a description for the entire object at run time that displays regardless of what property is selected within the property grid?
For example, if I had the following class
public class Person
{
public String Name { get; set; }
public String Age { get; set; }
public Person(String n, int a)
{
this.Name = n;
this.age = a;
}
public Person()
{
}
}
and I created a Person object in the following manner
Person Frank = new Person(Frank, 22);
and displayed that object in a property grid like so
propertyGrid1.SelectedObject = Frank;
I would like the ability to provide a description for the entire object rather than the name and age attributes of the Person class. And, because I want the description to pertain to the Frank object in particular, I would like to be able to set this description not only based on what type of object is selected, but the particular instance of that object. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CodeNaked 作为正确答案。 PropertyGrid 仅显示当前所选属性的描述,而不是整个实例的描述是有意义的。会有什么好处?如果您确实需要根据目标实例显示消息,为什么不在网格的顶部或底部创建标签呢?它的内容可以基于您的自定义属性或您自己的 DescriptionProvider...
CodeNaked as the right answer. It makes sense for a PropertyGrid to only display a description for the property that is currently selected, not for the whole instance. What would be the benefit? If you really need to display a message based on the targetted instance, why not create a label on top or bottom of the grid? Its content could be based on a custom attribute of yours or on your own DescriptionProvider...
PropertyGrid 仅显示属性的描述,而不显示对象的描述。也就是说,您可以在您的对象上实现 ICustomTypeDescriptor ,并且重写 GetProperties 方法。您可以在此处注入自定义 DescriptionAttribute。
有关此界面的较长教程可以在此处和此处。
The PropertyGrid only shows descriptions for the properties, not the object. That said, you could implement ICustomTypeDescriptor on your object and override the GetProperties methods. There you could inject a custom DescriptionAttribute.
A longer tutorial on this interface can be found here and here.