How to change the display name of a object's property in PropertyGrid

X

X.W.

Hi group,

I have a class like this:
class Person
{
public uint Age
{
get{...}
set{...}
}
}

Thus a PropertyGrid displays the Person.Age property as "Age".
But I want to display it in chinese, but not the english word "Age", and I
don't want to change this class's definition .
Is it possible?

Thank you very much!
 
X

X.W.

Today I Find that this feature is included by .NET Framewoke 2.0.
By "DisplayName" Attribute:

[DisplayName("My Age")]
public uint Age
{
get{...}
set{...}
}

It seems that it is impossible in .NET Framewoke 1.1 ...?
 
C

Chris Dunaway

It is possible.

The steps I followed are:

1. Create an attribute class (inherited from Attribute) for the
DisplayName

2. Create a class that inherits from PropertyDescriptor and override
the
DisplayName property to return what you want to appear in the
PropertyGrid

3. Create a class that implements the ICustomTypeDescriptor interface.
Implement the GetProperties functions to return a
PropertyDescriptorCollection with instances of the class defined in
step 2.

4. Finally, create the class you want to display in the PropertyGrid
and
make sure it inherits from the class defined in step 3 above. You can
also
combine steps 3 and 4 by by just implementing the ICustomTypeDescriptor
interface in your class.

When you assign an instance of that class to the SelectedObject
property of
the PropertyGrid, the grid will show your custom property names.

The following article helped me immensely:

http://www.codeproject.com/cs/miscctrl/globalizedpropertygrid.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top