Is it any easy way to show enum DisplayValues from enum Description attribute?
publicenumObjectType
{
[Description("Textual information in the Html format")]
TextHtml,
[Description("Textual information in the Xaml format")]
TextXaml,
[Description("Binary Data")]
Binary
}
1 Answer, 1 is accepted
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 28 Oct 2019, 11:08 AM
Hello, Emin,
You can handle the RadDataEntry.EditorInitialized event and subscribe to the VisualListItemFormatting event of RadDropDownList. Thus, you can customize the Text of each visual item in the drop down:
publicRadForm1()
{
InitializeComponent();
this.radDataEntry1.EditorInitialized += radDataEntry1_EditorInitialized;
Item item = new Item(123,"Test", ObjectType.TextXaml);
this.radDataEntry1.DataSource = item;
}
privatevoidradDataEntry1_EditorInitialized(object sender, EditorInitializedEventArgs e)
{
RadDropDownList ddl = e.Editor as RadDropDownList;
if (ddl != null)
{
ddl.VisualListItemFormatting -= ddl_VisualListItemFormatting;
ddl.VisualListItemFormatting += ddl_VisualListItemFormatting;
}
}
privatevoidddl_VisualListItemFormatting(object sender, VisualItemFormattingEventArgs args)
{
if (args.VisualItem.Data.Tag == null)
{
args.VisualItem.Data.Tag = GetDescription((ObjectType)args.VisualItem.Data.Value);
}
args.VisualItem.Text = args.VisualItem.Data.Tag.ToString();
}
publicclassItem
{
publicint Id { get; set; }
publicstring Name { get; set; }
public ObjectType Type { get; set; }
publicItem(int id, string name, ObjectType type)
{
this.Id = id;
this.Name = name;
this.Type = type;
}
}
publicenum ObjectType
{
[Description("Textual information in the Html format")]
TextHtml,
[Description("Textual information in the Xaml format")]
TextXaml,
[Description("Binary Data")]
Binary
}
publicstaticstringGetDescription (ObjectType objValue)
{
Type type = objValue.GetType();
Array values = System.Enum.GetValues(type);
foreach (int val in values)
{
if (val == (int)objValue )
{
var memInfo = type.GetMember(type.GetEnumName(val));
var descriptionAttribute = memInfo[0]
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.FirstOrDefault() as DescriptionAttribute;
if (descriptionAttribute != null)
{
return descriptionAttribute.Description;
}
}
}
returnnull; // could also return string.Empty
}
I have researched in general programming forums to find a suitable solution how to extract the description defined for each enum value: https://www.codeproject.com/Articles/19980/Data-Binding-an-Enum-with-Descriptions You can integrate this approach and bind the RadDropDownList to a collection that would use the description for DisplayMember.
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers.Learn More.