Search code examples
c#wpfcomboboxenums

adding enum values to a simple combobox


i have a really simple question to ask about C# and WPF. My Question will follow after this attempt of mine:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var item in Races)
            {
                cbRace.Items.Add(item);
            }
        }
    }

    enum Races
    {
        Human=1,
        Dwarf,
        Elf,
        Orc,
        Goblin,
        Vampire,
        Centaur
    }

Ok so, my question is how will I add the values(e.g. Human,dwarf,elf....) into the combo box: cbRace? sorry I'm new to C# so i would rally appreciate it if someone can help me out :), thanks in advance.


Solution

  • private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        cbRace.ItemsSource = Enum.GetValues(typeof(Races));
    }