Question

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 ?

Was it helpful?

Solution

This should work:

public class FruitConverter : TypeConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        return fruits[(uint)value];
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top