简体   繁体   English

c#propertygrid从uint转换为字符串

[英]c# propertygrid convert from uint to string

I can't convert showing property from uint to string format in PropertyGrid control. 我无法在PropertyGrid控件中将显示属性从uint转换为string格式。 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 ? 但是属性FruitTypeCode仍然显示为uint而不是string ,我做错了什么?

This should work: 这应该工作:

public class FruitConverter : TypeConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        return fruits[(uint)value];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM