c# Property Grid – Getting a combo box
by brian on Oct.31, 2008, under Uncategorized
You’d think getting a combox display into the property grid would be really simple to do since it’s used everywhere in Visual Studio 2008. And although it isn’t horrible it isn’t exactly straight forward. I found a couple of dead-ends, poorly explained, or difficult to find the pith of examples. Finally I ran across this one at http://gaaton.blogspot.com/ It’s nice and concise, easy to understand, and easy to adapt. I’m going to put in my sample which essentially taken from gaaton.
internal class ProtcolConverter : StringConverter
{
public override bool
GetStandardValuesSupported(
ITypeDescriptorContext context)
{
//True - means show a Combobox
//and False for show a Modal
return true;
}
public override bool
GetStandardValuesExclusive(
ITypeDescriptorContext context)
{
//False - a option to edit values
//and True - set values to state readonly
return true;
}
public override StandardValuesCollection
GetStandardValues(
ITypeDescriptorContext context)
{
return new StandardValuesCollection(
new string[] { "net.tcp", "http" });
}
}
And finally the we need to attach the string converter to the property by the following.
[TypeConverter(typeof(ProtcolConverter))]
public string ServiceProtocol
{
get { return MediaServiceConfig.WCFProtocol; }
set { MediaServiceConfig.WCFProtocol = value; }
}
Enjoy.
