Crudler Crudler - 28 days ago 13
C# Question

Windows Phone 8 - enums & DataAnnotations

Were this not a window phone 8 project, If I wanted to display a human readable version of an enum I would use a niftt DataAnnotation like such

public enum InstallationType
{
[Display(Description="Forward of Bulk Head")]
FORWARD = 0,

[Display(Description="Rear of Bulk Head")]
REAR = 1,

[Display(Description="Roof Mounted")]
ROOF = 2,
}


and pull them out using something to the effect of:

public static string GetDescriptionFromEnumValue(Enum value)
{
DisplayAttribute attribute = value.GetType()
.GetField(value.ToString())
.GetCustomAttributes(typeof(DisplayAttribute), false)
.SingleOrDefault() as DisplayAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}


Easy,
But my problem is that System.ComponentModel.DataAnnotation is not available in Windows Phone. I have tried using a Portable Class Library but when I target WP8 I loose the ability to hit that namespace.

What other options are there?

Answer

System.ComponentModel.DataAnnotation is not available in Windows Phone , you can make use of Linq. Follow this example to bind enum,

Missing in wp8

Binding enum in wp8

Comments