Reputation: 1457
I can't convert showing property from uint
to string
format in PropertyGrid
control. This is what I do:
var fruits = new SortedDictionary<uint, string>
{
{0, "Apple"},
{1, "Orange"},
{3, "Watermelon"},
};
public class FruitConverter : StringConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{
if (sourceType == typeof(uint) && fruits.ContainsKey(sourceType))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture,
object value)
{
if (sourceType == typeof(uint) && fruits.ContainsKey(sourceType))
return fruits[value];
return base.ConvertFrom(context, culture, value);
}
}
public class Fruit
{
[ReadOnly(true)]
[DisplayName("Type of fruit")]
[TypeConverter(typeof(FruitConverter))]
public uint FruitTypeCode { get; set; }
}
But property FruitTypeCode
is still is shown as uint
and not as a string
, what I did wrong ?
Upvotes: 0
Views: 979
Reputation: 139226
This should work:
public class FruitConverter : TypeConverter
{
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
return fruits[(uint)value];
}
}
Upvotes: 1