How to User TypeConverter to convert list<string> into dropdown in propertyGrid c#
See the question and my original answer on StackOverflowIf I understand well, you want to use a list of string dynamically created to define the value of a string.
Here is the class I use:
public class MyClass
{
private List<string> myNames;
public MyClass()
{
myNames = new List<string> { "jack", "pam", "phil", "suzan" };
}
[Browsable(false)]
public List<string> Names
{
get { return myNames; }
}
[TypeConverter(typeof(MyConverter))]
public string SelectedName { get; set; }
}
And here is the type converter:
public class MyConverter : TypeConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
// you need to get the list of values from somewhere
// in this sample, I get it from the MyClass itself
var myClass = context.Instance as MyClass;
if (myClass != null)
return new StandardValuesCollection(myClass.Names);
return base.GetStandardValues(context);
}
}
As you see, a converter has access to the property grid context to get the values from somewhere. This is what's displayed in this case: