Dmitry
Dmitry

Reputation: 423

PropertyGrid displays string property as object having Length property when using TypeConverter

Object of my custom class have several properties of string type. For one of the properties I use custom TypeConverter to display list of standart values for this property in PropertyGrid. Now I have problem that now this property is not displayed as simple string in PropertyGrid but as an object having Length property for itself, but I do not want it. How to fix it? enter image description here

My code is like this:

class ItemAdapter
{
    public ItemAdapter(Item pitem)
    {
        _item = pitem;
        ...
    }

    // This property shows as simple string in PropertyGrid
    public string Name
    {
        get
        {
            return _item.Name;
        }
        set
        {
            _item.Name = value;
        }
    }

    ...

    // This property shows Length subproperty in PropertyGrid
    [TypeConverter(typeof(ItemTypeConverter))]
    public string type
    {
        get
        {
            return _item.type;
        }
        set
        {
            _item.type = value;
        }
    }
}

I use TypeConverter only to display list of standart values, like this:

public class ItemTypeConverter : ExpandableObjectConverter
{
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    private readonly static List<string>  _standardValues = new List<string>()
    {
        "value1",
        "value2"
    };

    public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        return new System.ComponentModel.TypeConverter.StandardValuesCollection( _standardValues);
    }

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

Upvotes: 0

Views: 748

Answers (1)

Simon Mourier
Simon Mourier

Reputation: 139226

Replace ExpandableObjectConverter by TypeConverter.

Upvotes: 1

Related Questions