I have managed to create a solution for that.
Please find code below:
public class EnumCheckList : Checklist
{
public string Source { get; set; }
protected override void OnLoad(EventArgs e)
{
if (Sitecore.Context.ClientPage.IsEvent)
{
ReadCheckedItemsFromRequest();
}
else
{
if (Controls.Count != 0 || string.IsNullOrWhiteSpace(Source))
return;
try
{
var enumType = Type.GetType(Source);
if (enumType == null)
return;
RenderCheckListItems(enumType);
}
catch
{
Controls.Add(new ListItem() {Header = "Could not load enum for " + Source});
return;
}
}
base.OnLoad(e);
}
private void RenderCheckListItems(Type enumType)
{
foreach (Enum val in Enum.GetValues(enumType))
{
var item = new ChecklistItem()
{
ID = GetIdForEnum(val, enumType),
Header = val.GetDescription(),
Value = val.ToString(),
Checked = this.Value.ToLower().Contains(val.ToString().ToLower())
};
this.Controls.Add(item);
}
}
private void ReadCheckedItemsFromRequest()
{
var values = this.Context.Request.Form.AllKeys
.Where(key => key.HasValue())
.Where(key => key.Contains(this.ID))
.Select(GetValueFromID)
.ToList();
var value = string.Join(",", values);
Sitecore.Context.ClientPage.Modified = Value != value;
if (value != Value)
this.Value = value;
}
private string GetIdForEnum(Enum item, Type enumType)
{
return this.ID + "." + enumType.Namespace + "." + enumType.Name + "_" + item;
}
private string GetValueFromID(string id)
{
return id.Substring(id.IndexOf("_", StringComparison.InvariantCulture) + 1);
}
}
Hope this will help anyone.
Best regards