PropertyGrid control and drop-down lists

后端 未结 1 2087
情话喂你
情话喂你 2021-02-09 03:48

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:02

    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)
提交回复
热议问题