ExpandableObjectConverter Question

G

Guest

I've created a usercontrol with a Person property. The Person property
returns a Person object. I've also created a PersonConverter class which
inherits the ExpandableObjectConverter so the Person property is expandable
in the PropertyGrid. This all works fine.

I've also created another usercontrol that also has a Person property and
returns a Person object. But in this usercontrol I don't want the Person
object to be expandable.

What do I need to do in my PersonConverter class to enable the Person object
to be expandable in the PropertyGrid in my first usercontrol, but not in the
second usercontrol?

Thank you for your help.
 
M

Marc Gravell

On the second user-control (where you don't want it expanding), try
declaring the /property/ with the following:

[TypeConverter(typeof(TypeConverter))]
public Person SomeProperty {...}

This should tell it to use the default (non-expandable) converter for
this property. You could of course provide your own.
Full demonstration follows.

Marc

using System;
using System.ComponentModel;
using System.Windows.Forms;

public class SomeWrapper
{
[TypeConverter(typeof(ExpandableObjectConverter))]
public Person Expandable { get; private set; }
[TypeConverter(typeof(TypeConverter))]
public Person NonExpandable { get; private set; }

public SomeWrapper()
{
Expandable = new Person();
NonExpandable = new Person();
}
}
// [TypeConverter(typeof(ExpandableObjectConverter))]
public class Person
{
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}

static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
using(Form f = new Form())
using (PropertyGrid pg = new PropertyGrid())
{
pg.Dock = DockStyle.Fill;
f.Controls.Add(pg);
pg.SelectedObject = new SomeWrapper();
Application.Run(f);
}
}
}
 
V

VisualHint

Hello Mark,

In your PersonConverter's GetPropertiesSupported method, you could
interrogate the context.Instance to know if it can expand itself. It
could also make its decision depending on the type of the
context.Instance.

Best regards,

Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
Home of Smart FieldPackEditor.Net / DateTimePicker replacement (http://
www.visualhint.com/index.php/fieldpackeditor)
Home of Smart PropertyGrid for .Net and MFC (http://www.visualhint.com/
index.php/propertygrid)
Microsoft PropertyGrid Resource List - http://www.propertygridresourcelist.com
 

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