Force UITypeEditor to display a initial value for a Enum type
See the question and my original answer on StackOverflowThis is not a UITypeEditor issue, but a TypeConverter issue. What you can do is derive from the standard EnumConverter class, like this:
[TypeConverter(typeof(MyEnumConverter))]
public enum TestEnum
{
a = 1,
b = 2,
c = 4
}
public class MyEnumConverter : EnumConverter
{
public MyEnumConverter(Type type)
: base(type)
{
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
try
{
return base.ConvertTo(context, culture, value, destinationType);
}
catch
{
if (destinationType == typeof(string))
{
// or whatever you see fit
return "a";
}
throw;
}
}
}
PS: you can avoid the exception catch and do your own conversion, but it may be more difficult than it looks in the general case (depends on enum underlying type, etc.).