12

Alright so my terminology when it comes to C# isn't great, so I'll attempt to explain this with a small example. If you create a class which you are using within a PropertyGrid and you have the following values:

class Test
{
    public Point example { get; set; }
}

This will produce a PropertyGrid which has an expandable object "example" which has fields X and Y in order to create a "Point".

I'm attempting to create an object "name" which has fields "firstname" and "lastname", so I have:

class Test
{
    public Name example { get; set; }
}

public struct Name
{
    public string firstname { get; set; }
    public string lastname { get; set; }
}

This however isn't working as intended.

I think I need to override some method(s) in order to get this working, however since I don't really have the terminology down for PropertyGrids it is difficult for me to find a solution.

Any help would be great.

MvanGeest
  • 9,536
  • 4
  • 41
  • 41
tplaner
  • 8,363
  • 3
  • 31
  • 47

2 Answers2

15

After a lot of looking around I finally was able to figure it out, the missing keyword was "ExpandableObjectConverter."

Anyway, here is example code:

public Form1()
{
    InitializeComponent();

    Person x = new Person();
    propertyGrid1.SelectedObject = x;
}

public class Person
{
    public string Caption { get; set; }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class Name
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override string ToString()
        {
            return LastName + ", " + FirstName;
        }
    }

    private Name _name = new Name();

    public Name testName
    {
        get { return _name; }
    }
}

PropertyGrids sure are confusing.

tplaner
  • 8,363
  • 3
  • 31
  • 47
1

I believe that in this instance auto-Implimented property definitions

   public string Name { get; set; }

don't work for struct types but class types. In the first example Point is a class type.

Try

class Name
{
  public string FirstName {get;set;}
  public string LastName {get;set;}
}

then

class Test
{
  public Name example {get; set;}
}

may work.

ChrisBD
  • 9,104
  • 3
  • 22
  • 35
  • Yeah, I tried this, however it also doesn't work. The field is actually grayed out which leads me to think that something needs to be overridden. – tplaner May 24 '10 at 16:01
  • Hmm - I'll have to check on a machine that gives me VS. It may be down to the fact that a constructor is required – ChrisBD May 24 '10 at 16:27