繁体   English   中英

在PropertyGrid中绑定枚举集合

[英]Bind Enum Collection in PropertyGrid

我有以下列举。

public enum Digits
{One, Two, Three}

和具有两个条目的属性。

public List<Digits> DigitList{get;set;}
DigitList.Add(Digits.One); DigitList.Add(Digits.Three);

当此对象绑定到PropertyGrid时,它将显示为(集合),并且当打开(使用小的浏览按钮)时,将显示带有(无有用消息)的异常。 我对PropertyGrid如何解释枚举列表感到困惑。 我在寻找解决方案,但是我所能找到的只是关于如何绑定一个枚举值,而不是枚举列表。

您必须创建一个TypeConverter类,以帮助PropertyEditor将Enum解析为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;

    }

}

在将此声明添加到要在属性编辑器中解析的枚举之后。

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

       .....
}

最后一步是将其添加到将在propertyeditor中显示的组件的属性中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM