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

}

プロパティグリッド クラスのコード:

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

実行時にプロパティ グリッドで別の値を選択すると、上記の方法でもエラーが発生します。

質問は1、2回前に尋ねられましたが、完全な詳細ではなかったか、WPF +バインディングのみでした。Windows フォームを使用しています。

ConvertFrom(..) がまったく必要かどうかもわかりません。

4

2 に答える 2

6

TypeConverterが悪かった(TypeConverterからではなくEnumConverterから派生する必要がありました。

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

于 2012-09-19T13:56:34.563 に答える
0

リフレクションで試してください:

public static T GetEnumMemberFromName<T>(string name)
    {
        return (T)Enum.Parse(typeof(T), name);
    }

あなたのコードで:

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 T GetEnumMemberFromName<T>(string name)
    {
        return (T)Enum.Parse(typeof(T), name);
    }

}  

例:

eVisualType  myEnumMember = myVisualTypeConverterInstance.GetEnumMemberFromName<eVisualType>("Torch");
于 2012-09-19T10:41:20.473 に答える