user1765862
user1765862

Reputation: 14145

populate combobox with data from enum

I have enum

public enum MyEnum
{ 
   Choice = 1,
   Choicee = 2,
   Choiceee = 3
}

and I want to dynamically fill list with this enum values

var data = new List<ComboBoxItem>();

where ComboBoxItem has two properties, Id and Name. Id should be enum int value and name should be enum Value like Choice or Choicee, ...

Upvotes: 1

Views: 433

Answers (1)

Kamil Budziewski
Kamil Budziewski

Reputation: 23087

You can use Enum.GetValues for it:

var values = Enum.GetValues(typeof(SearchOption)).Cast<SearchOption>()
            .Select(x => new ComboBoxItem() { Id = (int)x, Name = x.ToString() }).ToList();

Upvotes: 5

Related Questions