Crudler Crudler - 1 year ago 81
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")]

[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()
.GetCustomAttributes(typeof(DisplayAttribute), false)
.SingleOrDefault() as DisplayAttribute;
return attribute == null ? value.ToString() : attribute.Description;

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download