PropertyGrid and interfaces

P

Peter Gomis

I've been experimenting with the PropertyGrid control and
have come across something peculiar. If the object you
want to display exposes its properties through an
interface, the PropertyGrid doesn't display anything. It
seems to work only if properties are exposes on the class
itself. Is there a way to overcome this?
 
J

Jay B. Harlow [MVP - Outlook]

Peter,
Can you explain better what you are attempting?

Can you post 10-15 lines demonstrating what you are doing. Include the class
& interface you are using, and how the properties are defined?

Hope this helps
Jay
 
P

Peter Gomis

Consider the following interface and class:

public interface ITest
{
string Name {get; set; }
int Age {get; set; }
}

public class Test : ITest
{
private string m_Name = "";
private int m_Age = 0;
private object m_Value;

string ITest.Name
{
get {return this.m_Name;}
set {this.m_Name = value;}
}
int ITest.Age
{
get {return this.m_Age;}
set {this.m_Age = value;}
}
public object Value
{
get {return this.m_Value;}
set {this.m_Value = value;}
}
}

If I instantiate one of these the PropertyGrid will only
show me the Value property and not the others.
 
J

Jay B. Harlow [MVP - Outlook]

Peter,
Is there a reason you are doing the Explicit interface member
implementations?
string ITest.Name
{

I'm sure that is the problem, as the PropertyGrid only sees public
properties. The Explicit interface implementation effectively makes the
properties private (hidden really).

One way to get this to work, would to make the properties public
public string Name
{

If you need the Explicit interface member implementations, then you will
need to implement the ICustomTypeDescriptor interface in your object, in the
GetProperties method, return a collection that has both your public
properties and your hidden properties (the properties belonging to the
interface).

Hope this helps
Jay
 
P

Peter Gomis

The example I posted was modeled after a series of classes
that use explicit interface implementation to facilitate
COM-Interop. I'll take a look a the ICustomTypeDescriptor
interface.
 

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