Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在PropertyGrid中绑定枚举集合_C#_Propertygrid - Fatal编程技术网

C# 在PropertyGrid中绑定枚举集合

C# 在PropertyGrid中绑定枚举集合,c#,propertygrid,C#,Propertygrid,我有以下列举 public enum Digits {One, Two, Three} 和一个包含两个条目的属性 public List<Digits> DigitList{get;set;} DigitList.Add(Digits.One); DigitList.Add(Digits.Three); 公共列表数字列表{get;set;} 数字列表。添加(数字1);数字列表。添加(数字3); 当此对象绑定到PropertyGrid时,它将显示为(集合),当它打开时(使用小浏览

我有以下列举

public enum Digits
{One, Two, Three}
和一个包含两个条目的属性

public List<Digits> DigitList{get;set;}
DigitList.Add(Digits.One); DigitList.Add(Digits.Three);
公共列表数字列表{get;set;}
数字列表。添加(数字1);数字列表。添加(数字3);
当此对象绑定到PropertyGrid时,它将显示为(集合),当它打开时(使用小浏览按钮),将显示异常(无有用消息)。我对PropertyGrid如何解释枚举列表感到困惑。
我搜索了一个解决方案,但我能找到的只是如何绑定枚举值,而不是枚举列表。

您必须创建一个TypeConverter类,以帮助PropertyEditor将枚举解析为PropertyEditor

样本类型转换器

public class FooDataTypeConverter : TypeConverter
{
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        return base.GetStandardValues(context);
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
            return true;

        return (sourceType.Equals(typeof(Enum)));
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return (destinationType.Equals(typeof(String)));
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value is string)
        {
            return (ValidationDataType)Enum.Parse(typeof(ValidationDataType), value.ToString(), true);
        }

        return base.ConvertFrom(context, culture, value);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (!destinationType.Equals(typeof(string)))
            throw new ArgumentException("Can only convert to string", "destinationType");

        if (!value.GetType().BaseType.Equals(typeof(Enum)))
            throw new ArgumentException("Can only convert an instance of enum", "value");

        string name = value.ToString();
        object[] attr =
            value.GetType().GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);

        return (attr.Length > 0) ? ((DescriptionAttribute)attr[0]).Description : name;

    }

}
之后,将此声明添加到要在propertyeditor中解析的枚举中

    [TypeConverter(typeof(FooDataTypeConverter ))]
    public enum ValidationDataType
    {
        /// <summary>
        /// None
        /// </summary>
        [Description("None")]
        None,

       .....
}

检查类名是否一致。类型转换器名为
FooDataTypeConverter
,但用作
eposodedatatypeconverter
这是一个示例代码,给出了总体思路,我更改了某些具有公司前缀的类的名称,这是一个比我那样做时更好的起点。@CheGueVerra当属性“Type”的类型为“ValidationDataType”(枚举)时,它工作得很好。但当属性类型为“列表”时,它不起作用<代码>公共列表类型{}用新代码更新您的问题,我不记得为任何属性设置列表代码不起作用。名字上的错误从未被纠正过
[Category("Behavior")]
[Description("Gets or sets the type of data that will be compared")]
[TypeConverter(typeof(DataTypeConverter))]
[EditorAttribute(typeof(ValidatorTypeEditor), typeof(System.Drawing.Design.UITypeEditor))]
public ValidationDataType Type
{
    get { return this.type; }
    set 
    { 
        this.type = value;
        if (this is RangeValidator)
        {
            this.SetRange();
        }
    }
}