Sometimes we need add a custom Dropdown, , that can mean a lot of things
In the example below I had to add an dropdown where the user can select the “kind” Icon that will be used on the block
So first thing to add on your project is the property itself (The code below was tested in EPISERVER 11)
[BackingType(typeof(PropertyNumber))]
[EditorDescriptor(EditorDescriptorType = typeof(EnumEditorDescriptor<IconType>))]
pblic virtual IconType ServiceAreaIcon { get; set; }
The second piece, is the Dropdown values
public enum IconType
{
Eletrocardio = 0,
Phone = 1,
FirstAidKit = 2
}

and the part 3 it will be the base , where the magic happens
Now, copy the whole class below and add to your project
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Framework.Localization;
using EPiServer.Shell.ObjectEditing;
using EPiServer.Shell.ObjectEditing.EditorDescriptors;
namespace MyProject.Models.Blocks.Base
{
public class EnumSelectionFactory<TEnum> : ISelectionFactory
{
public IEnumerable<ISelectItem> GetSelections(
ExtendedMetadata metadata)
{
var values = Enum.GetValues(typeof(TEnum));
foreach (var value in values)
{
yield return new SelectItem
{
Text = GetValueName(value),
Value = value
};
}
}
private string GetValueName(object value)
{
var staticName = Enum.GetName(typeof(TEnum), value);
string localizationPath = string.Format(
"/property/enum/{0}/{1}",
typeof(TEnum).Name.ToLowerInvariant(),
staticName.ToLowerInvariant());
string localizedName;
if (LocalizationService.Current.TryGetString(
localizationPath,
out localizedName))
{
return localizedName;
}
return staticName;
}
}
public class EnumEditorDescriptor<TEnum> : EditorDescriptor
{
public override void ModifyMetadata(
ExtendedMetadata metadata,
IEnumerable<Attribute> attributes)
{
SelectionFactoryType = typeof(EnumSelectionFactory<TEnum>);
ClientEditingClass =
"epi-cms/contentediting/editors/SelectionEditor";
base.ModifyMetadata(metadata, attributes);
}
}
}
You must be logged in to post a comment.