TypeConverter用于Type对象的属性

时间:2017-06-19 14:33:46

标签: c# winforms typeconverter

我需要在propertygrid中正确显示一个对象。 我的班级看起来像这样:

public class PropertyItem
{
    public PropertyDescription PropertyDescription { get; set; }

    [Description("the value"), Browsable(true)]
    public object Value { get; set; }

    public PropertyItem(PropertyDescription propertyDescription, object value)
    {
        PropertyDescription = propertyDescription;
        Value = value;
    }

    public override string ToString()
    {
        return this.PropertyDescription.Name + ": " + PropertyDescription.Type + ": " + Value;
    }
}

Value属于object类型,无法更改。 PropertyDescription的类型为Value,可以是任何内容(stringintbool ...)

当我设置SelectedObject的{​​{1}}时,PropertyGrid始终处于停用状态。

如何撰写Value以将TypeConverter object转换为Value中的Type

1 个答案:

答案 0 :(得分:3)

为属性定义自定义类型转换器:

[TypeConverter(typeof(PropertyValueConverter))]
public object Value { get; set; }

并按照以下方式实施:

public class PropertyValueConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        var propItem = context.Instance as PropertyItem;
        return propItem != null && TypeDescriptor.GetConverter(propItem.PropertyDescription.Type).CanConvertFrom(context, sourceType)
            || base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        var propItem = context.Instance as PropertyItem;
        if (propItem != null)
            return TypeDescriptor.GetConverter(propItem.PropertyDescription.Type).ConvertFrom(context, culture, value);
        else
            return base.ConvertFrom(context, culture, value);
    }
}

这个测试代码对我有用:

var pi = new PropertyItem(new PropertyDescription { Type = typeof(int) }, 1);
propertyGrid1.SelectedObject = pi;

<强>更新

支持下拉列表(例如bool):

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        var propItem = context.Instance as PropertyItem;
        if (propItem != null)
            return TypeDescriptor.GetConverter(propItem.PropertyDescription.Type).GetStandardValues(context);
        else
            return base.GetStandardValues(context);
    }

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        var propItem = context.Instance as PropertyItem;
        if (propItem != null)
            return TypeDescriptor.GetConverter(propItem.PropertyDescription.Type).GetStandardValuesSupported(context);
        else
            return base.GetStandardValuesSupported(context);
    }

支持自定义可打开属性(例如Point):

    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
    {
        var propItem = context.Instance as PropertyItem;
        if (propItem != null)
            return TypeDescriptor.GetConverter(propItem.PropertyDescription.Type).GetProperties(context, value, attributes);
        else
            return base.GetProperties(context, value, attributes);
    }

    public override bool GetPropertiesSupported(ITypeDescriptorContext context)
    {
        var propItem = context.Instance as PropertyItem;
        if (propItem != null)
            return TypeDescriptor.GetConverter(propItem.PropertyDescription.Type).GetPropertiesSupported(context);
        return base.GetPropertiesSupported(context);
    }