PropertyGrid control and drop-down lists

后端 未结 1 817
自闭症患者
自闭症患者 2021-02-09 03:38

I wanted to create a drop-down list as the editor for a property; If I had only strings as entries for the dropdown list, this would work fine (Using a StringConverter). However

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-09 04:22

    I added the CanConvertFrom and ConvertFrom methods to your conversion class:

    class BarConverter : TypeConverter
    {
      public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
      {
        return true;
      }
    
      public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
      {
        return new StandardValuesCollection(barlist);
      }
    
      public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
      {
        if (sourceType == typeof(string))
        {
          return true;
        }
        return base.CanConvertFrom(context, sourceType);
      }
    
      public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
      {
        if (value is string)
        {
          foreach (Bar b in barlist)
          {
            if (b.barvalue == (string)value)
            {
              return b;
            }
          }
        }
        return base.ConvertFrom(context, culture, value);
      }
    }
    

    0 讨论(0)
提交回复
热议问题