Enum values in SfDropDownList
Hi,
Need to display enum values with Display Names.
For now it is shown in SfDropDownList as "ParticipantsReview" (not splitted words).
How can I do that?
public enum Type
{
[Display(Name = "Participants Review")]
ParticipantsReview,
[Display(Name = "New Participant")]
NewParticipant
}
@bind-Value="InsertModel.Type"
DataSource="Enum.GetNames(typeof(Type))"
TItem="string"/>
SIGN IN To post a reply.
3 Replies
1 reply marked as answer
BC
Berly Christopher
Syncfusion Team
February 10, 2021 04:41 PM UTC
Hi Roberta,
Greetings from Syncfusion support.
Based on the requested requirement, we have prepared the sample for DropDownList component with ENUM data and attached it below.
|
class="w-75 mt-5 container">
<SfDropDownList TItem="Person" TValue="Colors?" PopupHeight="230px" Placeholder="Race" @bind-Value="@person.FavoriteColor" DataSource="@colors">
<DropDownListFieldSettings Text="Name" Value="FavoriteColor"></DropDownListFieldSettings>
</SfDropDownList>
<h3>@person.FavoriteColor</h3>
</div>
@code{
public Person person { get; set; } = new Person();
public class Person
{
public string Name { get; set; }
public Colors? FavoriteColor { get; set; }
}
public enum Colors
{
[Display(Name = "The Color Red")]
Red,
[Display(Name = "The Color Orange")]
Orange,
[Display(Name = "The Color Green")]
Green,
[Display(Name = "The Color Blue")]
Blue
}
protected override void OnInitialized()
{
person.FavoriteColor = Colors.Orange;
}
public IEnumerable<Person> colors = GetEnumDisplayNames<Colors>();
public static List<Person> GetEnumDisplayNames<T>()
{
var type = typeof(T);
return Enum.GetValues(type)
.Cast<T>()
.Select(x => new Person
{
FavoriteColor = x as Colors?,
Name = type.GetMember(x.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()?.Name ?? x.ToString()
}).ToList();
}
} |
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Blazor_Dropdown-319839653
Regards,
Berly B.C
Marked as answer
SG
Sven G
April 26, 2024 02:33 PM UTC
This also works with a Dictionary
<SfDropDownList ID="Type" TItem="KeyValuePair<ProjectType, string>" @bind-Value="@project.Type" TValue="ProjectType?"
DataSource="_types"
FloatLabelType="FloatLabelType.Always" Placeholder="Project type">
<DropDownListFieldSettings Value="Key" Text="Value"></DropDownListFieldSettings>
</SfDropDownList>
public enum ProjectType
{
[Display(Name = "Type 1")]
Type1 = 0,
[Display(Name = "Type 2")]
Type2 = 1
}
private Dictionary<ProjectType, string> _types = EnumExtensions.ToDictionary<ProjectType>();
public static class EnumExtensions
{
public static string GetDisplayName(this Enum enumValue) => enumValue.GetType()
.GetMember(enumValue.ToString())
.FirstOrDefault()?
.GetCustomAttribute<DisplayAttribute>()?
.GetName() ?? string.Empty;
public static Dictionary<T, string> ToDictionary<T>() where T : struct
=> Enum.GetValues(typeof(T)).Cast<T>().ToDictionary(e => e, e => (e as Enum).GetDisplayName());
}
YS
Yohapuja Selvakumaran
Syncfusion Team
April 30, 2024 11:51 AM UTC
Hi Roberta,
We're delighted to hear that the information was helpful to you and that you were able to resolve the issue. Thank you for sharing the details about the enum working with a dictionary. If you have any further questions or need assistance in the future, please get back to us.
Regards,
Yohapuja S
SIGN IN To post a reply.
- 3 Replies
- 4 Participants
- Marked answer
-
RO Roberta
- Feb 9, 2021 07:48 AM UTC
- Apr 30, 2024 11:51 AM UTC
6/02/2026 09:59:41 PM
Sun, 15 December 2024 03:30:00 UTC
Sun, 15 December 2024 03:30:00 AM
Wed, 16 Feb 2022 04:59:00 UTC