CodeCopy

October 6, 2009

WPF Databinding Enum with nice descriptions

Filed under: c#,wpf — sterndorff @ 17:28
Tags: , , ,

I have an Enum that I want to databind to a wpf drop down box. But I want to represent the enum with some nice descriptions instead of Enum.ToString().

1) The enum with the nice descriptions: MyEnum.cs

    public enum MyEnum
    {
        [Description("Default value")] // Default value if enum is nullable and used in databinding
        DefaultValue,
        [Description("Value1 description")]
        Value1,
        [Description("Value2 description")]
        Value2
    }

(more…)

September 24, 2009

Converting Nullable<int> to Nullable<Enum>

Filed under: c# — mazzoo @ 13:06
Tags: , , ,

Here is an example on how you identify a Nullable enum and convert an Nullable<int> (int? ) to Nullable<Enum> ( Enum? ).


// type = typeof(MyEnum);

if (type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
    if (type.GetGenericArguments().Length > 0 && type.GetGenericArguments()[0].IsEnum)
    {
        int? intValue = 1;
        if (intValue.HasValue)
        {
            TypeConverter convert = TypeDescriptor.GetConverter(type);
            return convert.ConvertFrom(intValue.Value + "");
        }
        return null;
    }
}

September 7, 2009

Binary Enum Issues

Filed under: c# — mazzoo @ 07:19
Tags: , , ,

Ever wondered how Binary Enums works ?

Binary enums are enums build up with the values 1, 2, 4, 8, 16, 32, 64, 128, 256, etc. The idea of binary enums are to be able to check if one or more of the enum values are set in a value.

An example:

Vehicle = 1, Car = 2, Motorcycle = 4

Value 3 => Vehicle + Car
(more…)

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.