【问题标题】:TypeConverter for property of Type objectTypeConverter 用于 Type 对象的属性
【发布时间】:2017-06-19 14:33:46
【问题描述】:

我需要在属性网格中正确显示一个对象。 我的课是这样的:

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...)

当我设置PropertyGridSelectedObject 时,Value 总是被禁用。

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

【问题讨论】:

  • 检查this是否可以帮助您
  • 显示不是主要的问题。但是,不知何故,有人将不得不在输入值时确定值的类型。 “0”的类型是什么?这是错误的做法。
  • 嗨,汉斯,我认为我的列表将包括正确的值和正确的类型。你说的是真的,但我现在没有考虑。谢谢你的提示

标签: c# winforms typeconverter


【解决方案1】:

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

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

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

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

【讨论】:

  • 如果我检查 bool 类型的值,propertygrid 将显示该值,但不会在只有 True 或 False 的下拉列表中显示,但它将是一个空文本框
  • pi = new PropertyItem(new PropertyDescription { Type = typeof(bool) }, true); 对我有用:它不会显示空文本框,我可以编辑该值。要提供 bool 常量的下拉列表,请以相同的方式覆盖 GetProperties
  • 您介意编辑上面的答案以包含您所说的 GetProperties 覆盖吗?
  • 谢谢...我用点测试了它,它工作...通过布尔我仍然有一些问题,它不显示下拉列表但我会检查它并让你知道..谢谢再次
猜你喜欢
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多