【发布时间】:2010-10-28 18:13:13
【问题描述】:
很多人已经回答了如何将枚举绑定到 WinForms 中的组合框的问题。是这样的:
comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));
但是如果不能设置要显示的实际值,那是毫无用处的。
我试过了:
comboBox1.SelectedItem = MyEnum.Something; // Does not work. SelectedItem remains null
我也试过了:
comboBox1.SelectedIndex = Convert.ToInt32(MyEnum.Something); // ArgumentOutOfRangeException, SelectedIndex remains -1
有人知道怎么做吗?
【问题讨论】:
-
为什么不试试 ComboBox.SelectedValue 呢?
-
如果你的问题已经得到解答,你真的应该选择一个答案。
-
数据绑定枚举的点不是很清楚。枚举在运行时可能不会改变。您还可以编写一个扩展方法,用枚举的所有值填充组合框的项目集合。
-
@OliverFriedrich
SelectedValue给我带来了InvalidOperationException。 "不能在ListControl中设置SelectedValue和空的ValueMember。"
标签: c# .net winforms combobox enums