是否可以创建一个通用的 Int-to-Enum 转换器?

发布于 2024-11-30 15:42:52 字数 393 浏览 1 评论 0原文

我希望能够将

<DataTrigger Binding="{Binding SomeIntValue}" 
             Value="{x:Static local:MyEnum.SomeValue}">

如果 int 值等于 (int)MyEnum.Value

其解析为 True我知道我可以制作一个返回 (MyEnum)intValueConverter ,但是我必须为我在 DataTriggers 中使用的每个 Enum 类型制作一个转换器。

有没有一种通用的方法来创建一个转换器来为我提供这种功能?

I'd like to be able to say

<DataTrigger Binding="{Binding SomeIntValue}" 
             Value="{x:Static local:MyEnum.SomeValue}">

and to have it resolve as True if the int value is equal to (int)MyEnum.Value

I know I could make a Converter that returns (MyEnum)intValue, however then I'd have to make a converter for every Enum type I use in my DataTriggers.

Is there a generic way to create a converter that would give me this kind of functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

要走干脆点 2024-12-07 15:42:52

可以以可重用的方式在枚举值与其基础整型类型之间创建转换器——也就是说,您不需要为每个枚举类型定义新的转换器。为此,已为 ConvertConvertBack 提供了足够的信息。

public sealed class BidirectionalEnumAndNumberConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return null;

        if (targetType.IsEnum)
        {
            // convert int to enum
            return Enum.ToObject(targetType, value);
        }

        if (value.GetType().IsEnum)
        {
            // convert enum to int
            return System.Convert.ChangeType(
                value,
                Enum.GetUnderlyingType(value.GetType()));
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // perform the same conversion in both directions
        return Convert(value, targetType, parameter, culture);
    }
}

调用时,此转换器纯粹根据 valuetargetType 值在 int/enum 值之间翻转值的类型。没有硬编码的枚举类型。

It is possible to create a converter between enum values and their underlying integral types in a reusable way -- that is, you don't need to define a new converter for each enum types. There's enough information provided to Convert and ConvertBack for this.

public sealed class BidirectionalEnumAndNumberConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return null;

        if (targetType.IsEnum)
        {
            // convert int to enum
            return Enum.ToObject(targetType, value);
        }

        if (value.GetType().IsEnum)
        {
            // convert enum to int
            return System.Convert.ChangeType(
                value,
                Enum.GetUnderlyingType(value.GetType()));
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // perform the same conversion in both directions
        return Convert(value, targetType, parameter, culture);
    }
}

When invoked, this converter flips the value's type between int/enum value based purely on the value and targetType values. There are no hard-coded enum types.

乱了心跳 2024-12-07 15:42:52

我想我发现

我只需要设置我的 ConverterParameter 而不是 Value 等于我正在寻找的枚举,并评估 True/False

<DataTrigger Value="True"
             Binding="{Binding SomeIntValue, 
                 Converter={StaticResource IsIntEqualEnumConverter},
                 ConverterParameter={x:Static local:MyEnum.SomeValue}}">

转换器

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (parameter == null || value == null) return false;

    if (parameter.GetType().IsEnum && value is int)
    {
        return (int)parameter == (int)value;
    } 
    return false;
}

I think I figured it out

I just needed to set my ConverterParameter instead of the Value equal to the Enum I am looking for, and evaluate for True/False

<DataTrigger Value="True"
             Binding="{Binding SomeIntValue, 
                 Converter={StaticResource IsIntEqualEnumConverter},
                 ConverterParameter={x:Static local:MyEnum.SomeValue}}">

Converter

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (parameter == null || value == null) return false;

    if (parameter.GetType().IsEnum && value is int)
    {
        return (int)parameter == (int)value;
    } 
    return false;
}
为人所爱 2024-12-07 15:42:52

您还可以采取相反的方式,使用自定义标记扩展将枚举转换为 int 作为值。

示例

<DataTrigger Binding="{Binding Path=MyNumber}"
             Value="{Markup:EnumToInt {x:Static Visibility.Visible}}">

EnumToIntExtension

public class EnumToIntExtension : MarkupExtension
{
    public object EnumValue
    {
        get;
        set;
    }
    public EnumToIntExtension(object enumValue)
    {
        this.EnumValue = enumValue;
    } 
    public override object ProvideValue(IServiceProvider provider)
    {
        if (EnumValue != null && EnumValue is Enum)
        {
            return System.Convert.ToInt32(EnumValue);
        }
        return -1;
    }
}

You could also go the other way around and convert the enum to int for the Value using a custom Markup Extension.

Example

<DataTrigger Binding="{Binding Path=MyNumber}"
             Value="{Markup:EnumToInt {x:Static Visibility.Visible}}">

EnumToIntExtension

public class EnumToIntExtension : MarkupExtension
{
    public object EnumValue
    {
        get;
        set;
    }
    public EnumToIntExtension(object enumValue)
    {
        this.EnumValue = enumValue;
    } 
    public override object ProvideValue(IServiceProvider provider)
    {
        if (EnumValue != null && EnumValue is Enum)
        {
            return System.Convert.ToInt32(EnumValue);
        }
        return -1;
    }
}
乖乖 2024-12-07 15:42:52

过去我们也曾多次想这样做,因此我们构建了几个扩展方法(在 int、long 等上)来帮助我们解决这个问题。所有这些的核心都是在单个静态泛型 TryAsEnum 方法中实现的:

    /// <summary>
    /// Helper method to try to convert a value to an enumeration value.
    /// 
    /// If <paramref name="value"/> is not convertable to <typeparam name="TEnum"/>, an exception will be thrown
    /// as documented by Convert.ChangeType.
    /// </summary>
    /// <param name="value">The value to convert to the enumeration type.</param>
    /// <param name="outEnum">The enumeration type value.</param>
    /// <returns>true if value was successfully converted; false otherwise.</returns>
    /// <exception cref="InvalidOperationException">Thrown if <typeparamref name="TEnum"/> is not an enum type. (Because we can't specify a generic constraint that T is an Enum.)</exception>
    public static bool TryAsEnum<TValue, TEnum>( TValue value, out TEnum outEnum ) where TEnum : struct
    {
        var enumType = typeof( TEnum );

        if ( !enumType.IsEnum )
        {
            throw new InvalidOperationException( string.Format( "{0} is not an enum type.", enumType.Name ) );
        }

        var valueAsUnderlyingType = Convert.ChangeType( value, Enum.GetUnderlyingType( enumType ) );

        if ( Enum.IsDefined( enumType, valueAsUnderlyingType ) )
        {
            outEnum = (TEnum) Enum.ToObject( enumType, valueAsUnderlyingType );
            return true;
        }

        // IsDefined returns false if the value is multiple composed flags, so detect and handle that case

        if( enumType.GetCustomAttributes( typeof( FlagsAttribute ), inherit: true ).Any() )
        {
            // Flags attribute set on the enum. Get the enum value.
            var enumValue = (TEnum)Enum.ToObject( enumType, valueAsUnderlyingType );

            // If a value outside the actual enum range is set, then ToString will result in a numeric representation (rather than a string one).
            // So if a number CANNOT be parsed from the ToString result, we know that only defined values have been set.
            decimal parseResult;
            if( !decimal.TryParse( enumValue.ToString(), out parseResult ) )
            {
                outEnum = enumValue;
                return true;
            }
        }

        outEnum = default( TEnum );
        return false;
    }

此实现处理具有任何基础类型的枚举,以及使用 [Flags] 属性定义的枚举。

We've wanted to do this a few times in the past as well, so we built several extension methods (on int, long, etc) to help us out. The core of all of these is implemented in a single static generic TryAsEnum method:

    /// <summary>
    /// Helper method to try to convert a value to an enumeration value.
    /// 
    /// If <paramref name="value"/> is not convertable to <typeparam name="TEnum"/>, an exception will be thrown
    /// as documented by Convert.ChangeType.
    /// </summary>
    /// <param name="value">The value to convert to the enumeration type.</param>
    /// <param name="outEnum">The enumeration type value.</param>
    /// <returns>true if value was successfully converted; false otherwise.</returns>
    /// <exception cref="InvalidOperationException">Thrown if <typeparamref name="TEnum"/> is not an enum type. (Because we can't specify a generic constraint that T is an Enum.)</exception>
    public static bool TryAsEnum<TValue, TEnum>( TValue value, out TEnum outEnum ) where TEnum : struct
    {
        var enumType = typeof( TEnum );

        if ( !enumType.IsEnum )
        {
            throw new InvalidOperationException( string.Format( "{0} is not an enum type.", enumType.Name ) );
        }

        var valueAsUnderlyingType = Convert.ChangeType( value, Enum.GetUnderlyingType( enumType ) );

        if ( Enum.IsDefined( enumType, valueAsUnderlyingType ) )
        {
            outEnum = (TEnum) Enum.ToObject( enumType, valueAsUnderlyingType );
            return true;
        }

        // IsDefined returns false if the value is multiple composed flags, so detect and handle that case

        if( enumType.GetCustomAttributes( typeof( FlagsAttribute ), inherit: true ).Any() )
        {
            // Flags attribute set on the enum. Get the enum value.
            var enumValue = (TEnum)Enum.ToObject( enumType, valueAsUnderlyingType );

            // If a value outside the actual enum range is set, then ToString will result in a numeric representation (rather than a string one).
            // So if a number CANNOT be parsed from the ToString result, we know that only defined values have been set.
            decimal parseResult;
            if( !decimal.TryParse( enumValue.ToString(), out parseResult ) )
            {
                outEnum = enumValue;
                return true;
            }
        }

        outEnum = default( TEnum );
        return false;
    }

This implementation handles Enums with any underlying type, as well as enums defined with the [Flags] attribute.

贪恋 2024-12-07 15:42:52

您可以对 int 值执行 ToString() 操作,然后将其传递到静态 Enum.Parse 或 Enum.TryParse 方法,该方法采用您关心的枚举类型并返回适当的值。

但这不是一个完美的解决方案,因为它不适用于表示多个枚举值的二进制 ORing 的整数

You could do a ToString() on the int value and then pass that into the static Enum.Parse or Enum.TryParse method which takes the enum type you care about and returns the appropriate value.

This isn't a perfect solution though because it won't work with integers that represent the binary ORing of multiple enum values

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文