PropertyGrid and interfaces

  • Thread starter Thread starter Peter Gomis
  • Start date Start date
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?
 
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
 
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.
 
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
 
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.
 
Back
Top