Binding to Enums

by Andrej Tozon 16. February 2009 05:13

I’ve seen numerous questions regarding data binding to Enums, together with many solutions on how to do it, but the question I’m asking myself is – do I really want to bind anything to an Enum?

I like to think about Enums purely as a coding aid – to help programmer code with some descriptive names instead of messing with pure numeric values. Similar to what constants are for, except Enums provide a set of values for describing a single parameter.

Instead of:

product.Quality = 3;

the programmer would write:

product.Quality = Quality.Good;
 
… providing that Quality Enum is declared something like:
 
public enum Quality
{
    JustBad, Poor, OK, Good, Excellent
}

Of course, you could set your enum values to some concrete numbers, like:

public enum Quality
{
    JustBad = 1, Poor = 2, OK = 3, Good = 4, Excellent = 5
}

… but in most cases you shouldn’t need to. Like I said, I see Enums only as a before-compilation, human <-> code communication, and therefore I believe no part of Enum should ever see the light of day (i.e. be exposed to the UI). And with that, there goes the need for binding to Enums…

Why would I want to bind to Enums anyway? Enum enumerators have no description (other than their names). If you need to provide spaces or even support localized descriptions, you’ll need to extend them significantly, so why not create a new class anyway? Here’s what I use instead of an Enum:

public sealed class Quality
{
    public const int JustBad = 1;
    public const int Poor = 2;
    public const int OK = 3;
    public const int Good = 4;
    public const int Excellent = 5;

    public Dictionary<int, string> Collection { get; private set; }

    public Quality()
    {
        Collection = new Dictionary<int, string>()
        {
            {JustBad,   "Just bad :("},
            {Poor,      "Poor"},
            {OK,        "OK"},
            {Good,      "Good"},
            {Excellent, "Excellent!"},
        };
    }
}

The programmer would still write:

product.Quality = Quality.Good;

so it makes it easy to refactor existing code. Additionally, you can put in any description for the values, support localization, and it’s easy bindable in Xaml - declare the class instance as a local resource:

<local:Quality x:Key="quality" />
and bind away:
 
<ListBox DisplayMemberPath="Value" 
         ItemsSource="{Binding Collection, Source={StaticResource quality}}" />

Tags:

Development | Silverlight | WPF

Comments

2/16/2009 6:33:54 AM #

Bojan Vrhovnik

I agree..i'ts better to create static class with values and bind it to desired source. Enums and binding can provide additional headache, like suppouse localisation. It can be done, but that's crochet and you loose flexibility.

Bojan Vrhovnik Slovenia | Reply

2/16/2009 10:44:47 AM #

pingback

Pingback from silverlight-travel.com

Silverlight Travel » Binding to Enums

silverlight-travel.com | Reply

3/2/2009 2:01:42 PM #

pingback

Pingback from blog.lulutech.com

kevin Mocha - Binding to Enums

blog.lulutech.com | Reply

3/17/2009 10:50:27 AM #

joe

you wrote The programmer would still write:

product.Quality = Quality.Good;

But this doesn't seem to be true. You're trying to assign type int to type Quality.

joe United States | Reply

3/17/2009 2:27:10 PM #

Andrej

Joe,
you're right - you have to change the type of product's Quality property from enum to int. That's what I meant by refactoring existing code. Thanks for bringing that up!

Andrej Slovenia | Reply

3/27/2009 12:46:27 PM #

Dmitri

Umm, I disagree completely. What do you think DescriptionAttribute inside System.ComponentModel is for? That's right - precisely for this. Here, let me cut+paste an enum from a project of mine:

public enum SyntaxHighlighterOptions
{
  [Description("Do not use")]
  DoNotUse,
  [Description("C#")]
  CSharp
}

I bind this to a WPF property grid and get the Description values in a list box.

Dmitri Russia | Reply

3/28/2009 6:52:35 AM #

Andrej

Dmitri,
thanks, I know about DescriptionAttribute. If it works for you - by all means, use it.

Andrej Slovenia | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading





Andrej Tozon
Andrej Tozon's on Twitter View Andrej Tozon's profile on LinkedIn Subscribe to me on FriendFeed Andrej Tozon's Facebook profile

MVP - Client Application Developer

Microsoft Certified Solution Developer

MSN Alerts

Get help from Andrej Tozon!

 

Currently reading


Microsoft Silverlight 4 Data and Services Cookbook

Stay tuned for the review... In the mean time, you can read an excerpt from the book.

RecentComments

Comment RSS