Displaying Object Description within a PropertyGrid
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, i开发者_运维技巧f 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?
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.
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...
精彩评论