+ 3

Binding enums to combobox in WPF.

you know I've read articles about binding enums to combobox but I cannot understand ( in www.wpftutorial.com and stackoverflow.com) who can solcey problem? who can teach me how can I bind these to each other ?!

5th Jun 2018, 8:57 PM
Alireza Askarikheirabadi
Alireza Askarikheirabadi - avatar
2 odpowiedzi
+ 4
I faced this problem back then too, mate. The easiest way to do this is by telling the datasource that you gonna fill it with enum-values that got already declared. Here is the easiest example i can image: public Form1() { InitializeComponent(); fillCombobox(); } public void fillCombobox() { comboBox1.DataSource = Enum.GetValues(typeof(myEnum)); } public enum myEnum { zero = 0, one = 1, two = 2, three = 3 }; Here combobox1 is your blank combobox that will be filled by enum values stored in your public enum myEnum{}. Hopefully it helps a bit. KK.
11th Jun 2018, 3:54 PM
Kevin Kalb
Kevin Kalb - avatar
+ 1
This is difficult to do because the combobox does not understand that the enum is actually a list. So you have to make it a list for the combobox. Do you use MVVM ? Do you want to solve the problem in the XAML or in the code behind
10th Jun 2018, 8:45 PM
sneeze
sneeze - avatar