Enumerations and View Model

For using enumerations in view models please have a look on the EnumInfo abstract class.
It is a very convenient and simple approach.
    enum ActiveFlags
    {
        None = 0,
        Active = 1,
        Inactive = 2,
    }
    
    class ActiveFlagInfo : EnumInfo<ActiveFlags>
    {
        public ActiveFlagInfo(ActiveFlags value)
            : base(value)
        {
            // here you can localize or set user friendly name of the enum value
            if (value == ActiveFlags.Active)
                this.Name = "Active";
            else if (value == ActiveFlags.Inactive)
                this.Name = "Inactive";
            else if (value == ActiveFlags.None)
                this.Name = "(not set)";
        }
    }

   // usage of ActiveFlagInfo class 
   // you can use collection of ActiveFlagInfo for binding in your own view models
   // also you can use this ActiveFlagInfo as property for your  classes to wrap enum properties
   IEnumerable<ActiveFlagInfo> activeFlags = ActiveFlagInfo.GetEnumInfos(e => 
                    e == ActiveFlags.None ? null : new ActiveFlagInfo(e));

Last edited Feb 6, 2013 at 11:38 AM by dmitry_dk, version 2

Comments

No comments yet.