具有枚举值的 C# Propertygrid 组合框(Win Forms)

标签 c# windows forms enums propertygrid

public enum eVisualType
{
    None = 0, Torch = 1, Rune01, Rune02, Rune03, FireRed01,
    LaserBlackWhiteLeft, LaserBlackWhiteRight, LaserBlueRedLeft, LaserBlueRedRight,
    Wheel01, Wheel01a, Wheel02, BlinkingStar, MovingPillar
}

public class EnumTypeConverter : TypeConverter
{
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true; // True means show a combobox
    }
    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        return true; // True will limit to list. false will show the list, but allow free-formentry
    }
}

public class VisualTypeConverter : EnumTypeConverter
{
    public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        return new StandardValuesCollection(new eVisualType[] { eVisualType.BlinkingStar, eVisualType.FireRed01, eVisualType.LaserBlackWhiteLeft, eVisualType.LaserBlackWhiteRight, eVisualType.LaserBlueRedLeft, eVisualType.LaserBlueRedRight, eVisualType.MovingPillar, eVisualType.Rune01, eVisualType.Rune02, eVisualType.Rune03, eVisualType.Torch, eVisualType.Wheel01, eVisualType.Wheel01a, eVisualType.Wheel02 });
    }

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

}

propertygrid 类中的代码:

    private eVisualType m_VisualType = eVisualType.FireRed01;
    [CategoryAttribute("Basic"), DescriptionAttribute("The visual type.")]
    [TypeConverter(typeof(VisualTypeConverter))]
    [DisplayName("Visual Type")]
    public eVisualType VisualType
    {
        get { return m_VisualType; }
        set { m_VisualType = value; }
    }

在运行时在 propertygrid 中选择不同的值时,上面仍然会产生错误:“System.String”类型的对象无法转换为“[项目名称].eVisualType”类型。

该问题之前曾被问过一两次,但从未问过完整的细节,或者仅针对 WPF+binding。我使用 Windows 窗体。

我也不确定是否需要 ConvertFrom(..)。

最佳答案

TypeConverter 很糟糕(我需要从 EnumConverter 派生而不是 TypeConverter。

using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;

namespace [namespace]
{
    public class EnumTypeConverter : EnumConverter
    {
        private Type m_EnumType;
        public EnumTypeConverter(Type type)
            : base(type)
        {
            m_EnumType = type;
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destType)
        {
            return destType == typeof(string);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destType)
        {
            FieldInfo fi = m_EnumType.GetField(Enum.GetName(m_EnumType, value));
            DescriptionAttribute dna =
                (DescriptionAttribute)Attribute.GetCustomAttribute(
                fi, typeof(DescriptionAttribute));

            if (dna != null)
                return dna.Description;
            else
                return value.ToString();
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type srcType)
        {
            return srcType == typeof(string);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            foreach (FieldInfo fi in m_EnumType.GetFields())
            {
                DescriptionAttribute dna =
                (DescriptionAttribute)Attribute.GetCustomAttribute(
                fi, typeof(DescriptionAttribute));

                if ((dna != null) && ((string)value == dna.Description))
                    return Enum.Parse(m_EnumType, fi.Name);
            }
            return Enum.Parse(m_EnumType, (string)value);
        }
    }
}

http://www.codeproject.com/Articles/22717/Using-PropertyGrid

关于具有枚举值的 C# Propertygrid 组合框(Win Forms),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12492436/

相关文章:

html - 标签未在文本输入上方的左上角对齐

php - 更改表单选择时的样式表 onChange ="change()"

C# 格式化 MessageBox

c# - Azure 2012 年 10 月 sdk - 如何删除表实体而不先检索它?

c# - Entity Framework : read only property vs parameter-less method

php - Windows机器上的Hadoop Hello world

r - Rscript在带有单引号和双引号的窗口上的行为不一致

c++ - 如何确定正在使用哪个USB端口?

C#清除选择框并动态构建它——Windows窗体应用程序

forms - 如何从 HTML 表单调用 RESTful 服务?