0

我无法将显示属性从控件转换uintstring格式PropertyGrid。这就是我所做的:

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; }
}

但是属性FruitTypeCode仍然显示为uint而不是显示string,我做错了什么?

4

1 回答 1

1

这应该有效:

public class FruitConverter : TypeConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        return fruits[(uint)value];
    }
}
于 2013-07-30T16:10:17.720 回答