Editing a property of an object inside an object i

2019-07-27 11:25发布

问题:

I'm trying to make an object, configureable/editable with a propertygrid. This is all going well, except for objects inside objects.

I've got an object/class named "ContactInformation". And inside that object I've got an object named "Correspondence".

This is how that part looks:

[Browsable(false)]
public Correspondence Correspondence
{
    get;
    set;
}
public int CorrespondenceStatus 
{
    get { return this.Correspondence.Status; }
    set { this.Correspondence.Status = CorrespondenceStatus; }
}
public string CorrespondenceComment
{
    get { return this.Correspondence.Comment; }
    set { this.Correspondence.Comment = CorrespondenceComment; }
}
public DateTime CorrespondenceDate
{
    get { return this.Correspondence.LastSend; }
    set { this.Correspondence.LastSend = CorrespondenceDate; }
}

That way I can show the properties/variables of the object inside the object, in the propertygrid.

Anyway, when I edit the values now, and press enter, or click somewhere else, instead of keeping it the value I just typed in, it changes back..

Anyone got an idea why this is happening? Or maybe a better idea to show the properties of objects in objects in the propertygrid?

回答1:

To edit properties inside an object (this is what you see for example with the winform editor with properties like Font, or Padding, ... where you can "expand" the oject clicking on the 'plus' icon) , you can use the ExpandableObjectConverter class, like this:

[TypeConverter(typeof(ExpandableObjectConverter))]
public class Correspondence
{
...
}

and remove the Browsable(false) of course:

public Correspondence Correspondence
{
    get;
    set;
}