1

so I want to populate a combobox items source with the contents on an enum (and obviously retrieve the values later on).

Populating works fine with either of the two following approaches but neither matches what I want to achieve.

comboBox.ItemsSource = Enum.GetValues(typeof(VirtualKey));

I can retrieve the values without issue using a simple (VirtualKey)comboBox.SelectedItem. The problem is, the names in the dropdown menu are all unreadable. enter image description here

comboBox.ItemsSource = Enum.GetNames(typeof(VirtualKey));

Displays the names as intended but I can't retrieve the value like with the other instruction.

Any ideas how to solve the situation?

Fussinger
  • 433
  • 2
  • 11

2 Answers2

1

comboBox.ItemsSource = Enum.GetNames(typeof(VirtualKey));

Displays the names as intended but I can't retrieve the value like with the other instruction.

I don't know how are you getting the selected value, but for converting from enum value name to its value, you can use the Enum.Parse(Type, String) method.

Or you can bind the ComboBox.SelectedIndex to your enum variable for example using something like my EnumToIntConverter.

Community
  • 1
  • 1
Marian Dolinský
  • 3,312
  • 3
  • 15
  • 29
1

This puzzling behavior was also discussed here:

UWP - binding Enum differences

Even simple ToString() call overcomes this issue. For some reason however, some Windows Runtime enums in direct XAML binding show up with IReference.

Community
  • 1
  • 1
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91