CSLA .NET FAQ

Create a NVLB from an enum

Modified: 2009/10/24 23:41 by RockfordLhotka - Categorized as: Object design
Given an enum like

  public enum MyEnum
  {
    Red,
    Blue,
    Green
  }

You'd have a class like:

  [Serializable]
  public class MyEnumList : Csla.NameValueListBase
  {
    public static MyEnumList GetList()
    {
      return Csla.DataPortal.Fetch(new Criteria(typeof(MyEnumList)));
    }

    private MyEnumList()
    { /* require use of factory method */ }

    [Csla.RunLocal]
    protected override void DataPortal_Fetch(object criteria)
    {
      this.IsReadOnly = false;
      foreach (MyEnum item in Enum.GetValues(typeof(MyEnum)))
        Add(new Csla.NameValueListBase.NameValuePair(
          item, Enum.GetName(typeof(MyEnum), item)));
      this.IsReadOnly = true;
    }
  }

Then you can bind it like you'd bind any other object/list to a combobox, as long as the object's property is of type MyEnum:

    MyEnum _color;
    public MyEnum Color
    {
      get
      {
        CanReadProperty("Color", true);
        return _color;
      }
      set
      {
        CanWriteProperty("Color", true);
        if (!_color.Equals(value))
        {
          _color = value;
          PropertyHasChanged("Color");
        }
      }
    }

Edit

How can I load a NVL object from an enum in Silverlight?

http://forums.lhotka.net/forums/thread/32564.aspx

Edit

How can I localize the text?

this thread has some good information on localization.

All content and CSLA .NET is Copyright © 2009-2011 Marimer LLC