【问题标题】:PropertyGrid displays string property as object having Length property when using TypeConverter使用 TypeConverter 时,PropertyGrid 将字符串属性显示为具有 Length 属性的对象
【发布时间】:2014-07-03 06:51:00
【问题描述】:

我的自定义类的对象有几个字符串类型的属性。对于其中一个属性,我使用自定义 TypeConverter 在 PropertyGrid 中显示此属性的标准值列表。现在我有一个问题,现在这个属性不是在 PropertyGrid 中显示为简单的字符串,而是作为一个本身具有 Length 属性的对象,但我不想要它。如何解决?

我的代码是这样的:

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;
        }
    }
}

我只使用 TypeConverter 来显示标准值列表,如下所示:

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;
    }
}

【问题讨论】:

    标签: c# .net user-interface propertygrid


    【解决方案1】:

    ExpandableObjectConverter 替换为TypeConverter

    【讨论】:

    • 谢谢,解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多