views:

1085

answers:

1

I have a Property Grid in C#, loading up a 'PropertyAdapter' object (a basic wrapper around one of my objects displaying relevant properties with the appropriate tags)

I have a TypeConverter on one of the properties (DataType, that returns an enumeration of possible values) as I want to limit the values available to the property grid to Decimal and Integer, with the 2 methods as follows

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        return new StandardValuesCollection(new List<Constants.DataTypes>() { Constants.DataTypes.Decimal, Constants.DataTypes.Integer });
    }

This is displaying just as I want it on the property grid, and when I double click the property field in the property grid, it happily switches between Integer and Decimal. Similarily I can use the mouse wheel to scroll through the options in the property filed's combobox.

If I however use the property field as a Combo Box and select a value from the drop-down, I get the standard property grid error box with the error:

Object of type 'System.String' cannot be converted to type 'Pelion.PM3.Utils.Constants+DataTypes'.

I am assuming I can use the Converter overrides in the Type converter to trap these and convert them to an Enum of DataTypes, but why would the property grid fail when I select from the drop-down instead of double clicking or 'mouseewheeling' on the drop down?

+2  A: 

When selected from the combo box drop down, the value is returned as string. I am not sure why that is, but I've seen in happen before. I think that basically double clicking or scrolling the mousewheel changes values from the value collection, while selecting from the drop down is like editing the field value as a string. Then, you have the convert the value from a string to the enum value.

configurator