1

バインドされた整数値を列挙型テキストに変換する列挙型のパラメーターを渡す wpf コンバーターがあります。これを行うには、ジェネリックとして、渡された修飾型名の列挙型を取得する必要があります。

namespace SomeOtherProject.MyClass
//
public enum MyTypes
{
   MyType1 = 0,
   MyType2 = 100,
   MyType3 = 200,
   MyType4 = 300
}


namespace SomeProject.SomeClass
{
//
var typeName = SomeOtherProject.MyClass.MyTypes;
type = Type.GetType(typeName);

これは型を取得せず、null を生成します。

ご協力ありがとうございます

4

2 に答える 2

0

を使用して、任意のタイプtypeofの を取得できます。System.Type同じプロジェクトにあるかどうかは関係ありません (他のプロジェクトが参照されている限り)。

Type theType = typeof(SomeOtherProject.MyClass.MyTypes);
于 2013-08-08T23:47:47.010 に答える
0

これは、enum int 値からそのテキスト値への変換を生成するために使用したカスタム コンバーターです。

WPF Xaml gridviewdatacolumn スニペット

... DataMemberBinding="{Binding Path=ProductTypeId, Converter={StaticResource IntEnumValueToDisplayNameConverter1}, ConverterParameter=Solution1.SomePath.SomeOtherPath\, Solution1\.SomePath\, Version\=1\.0\.0\.0\, Culture\=neutral\, PublicKeyToken\=null}"

コンバーターのコード例:

namespace Solution1.SomePath.Converters
{
   using System;
   using System.Globalization;
   using System.Windows.Data;

   internal class IntEnumValueToDisplayNameConverter : IValueConverter
   {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
         try
         {
            if (value == null || parameter == null)
               return null;

            var type = GetTypeFromParameter(parameter);

            if (type == null)
               return null;

            var enumValue = Enum.Parse(type, value.ToString(), true);

            if (enumValue == null)
               return null;

            return enumValue.ToString();
         }
         catch (Exception)
         {
            return null;
         }
      }

      private static Type GetTypeFromParameter(object parameter)
      {
         if (parameter == null)
            return null;

         Type type;

         if (parameter is Type)
         {
            type = parameter as Type;

            if (type == null || !type.IsEnum)
               return null;
         }
         else if (parameter is string)
         {
            // Must be fully qualified assembly name to work
            // Example: Solution1.SomePath.SomeOtherPath, Solution1.SomePath, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
            var typeName = parameter as string;

            if (typeName.IsEmpty())
               return null;

            type = Type.GetType(typeName);

            if (type == null || !type.IsEnum)
               return null;
         }
         else
            return null;

         return type;
      }
   }
}
于 2013-08-13T19:56:09.603 に答える