0

I'm programming in WPF(C#). I populate a ComboBox with this function:

public static void PopulateComboBox(ComboBox cmb, Type type)
{
    foreach (string name in Enum.GetNames(type))
    {
        cmb.Items.Add(name);
    }
}

Now I need a method like this (as illustrated below) to get any enum as output:

public static enum PopulateComboBox(ComboBox cmb, string nameOfEnum, Type type)
{

}

How can I write such function?

Babak.Abad
  • 2,839
  • 10
  • 40
  • 74

2 Answers2

0

I would look into adding the enum values to the ComboBox directly instead of their names.

Another option would be Enum.Parse(Type enumType, string value).

TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
-1

Finally I found my answer in this page. My answer is:

public static T ToEnum<T>(this string value)
{
    return (T) Enum.Parse(typeof(T), value, true);
}

For example I call it in this way:

BorderType borderType = ToEnum<BorderType>("Constant");

where BorderType is an enum (from OpenCV);

Community
  • 1
  • 1
Babak.Abad
  • 2,839
  • 10
  • 40
  • 74