[英]Trying to bind a combobox to an enum in C#
I have a class that contains a property that is an enum: 我有一个包含包含枚举的属性的类:
public RaTypes RaBucket1Type { get; set; }
My enum is: 我的列举是:
public enum RaTypes
{
Red,
Yellow
}
I was able to bind a form's combobox data-source to the enum so that when I click on the drop-down, I see the enumerations: 我能够将表单的组合框数据源绑定到枚举,以便在单击下拉列表时看到枚举:
cmbBucket1Type.DataSource = Enum.GetValues(typeof(RaTypes));
When I load the form, I would like to populate the combo-box with the existing value. 加载表单时,我想用现有值填充组合框。 I have tried the following:
我尝试了以下方法:
cmbBucket1Type.DisplayMember = "TradeType";
cmbBucket1Type.ValueMember = "TradeEnumID";
cmbBucket1Type.SelectedValue = EditedAlgorithm.RaBucket1Type;
But this did not work. 但这没有用。
Also, I'm not sure I have implemented the ValueChanged event handler correctly either: 另外,我不确定我是否正确实现了ValueChanged事件处理程序:
EditedAlgorithm.RaBucket1Type = (RaTypes)((ComboBox)sender).SelectedItem;
Can someone help me understand: 有人可以帮助我了解:
Thanks -Ed 谢谢-编辑
UPDATES I have tried 我尝试过的更新
cmbBucket1Type.SelectedIndex = cmbBucket1Type.FindString(EditedAlgorithm.RaBucket1Type.ToString());
and 和
cmbBucket1Type.SelectedItem = EditedAlgorithm.RaBucket1Type;
Neither works. 都不行。
You can set the selectedValue like this: 您可以这样设置selectedValue :
cmbBucket1Type.SelectedValue = EditedAlgorithm.RaBucket1Type;
And you can handle the selected value when the combo change like this: 您可以在组合更改时处理选定的值,如下所示:
private void cmbBucket1Type_SelectedValueChanged(object sender, EventArgs e)
{
var selectedValue = cmbBucket1Type.SelectedValue;
}
I think you're using the terminology a little differently than normal, which makes it difficult to understand. 我认为您使用的术语与正常情况有所不同,这使您难以理解。
Normally, the terms Add , Populate , and Select are used to mean the following: 通常,术语Add , Populate和Select用来表示以下内容:
Having cleared that up, I assume following is what you want to do. 清除这些内容后,我假设您要执行以下操作。
ComboBox
with a set of values. ComboBox
。 In your case, values of RaType
Enum
. RaType
Enum
值。 SomeClass
. SomeClass
。 RaBucket1Type
property of the said class instance with an enum
value of your choice. enum
值初始化所述类实例的RaBucket1Type
属性。 I'll initialize it to Yellow
. Yellow
。 ComboBox
select the said value at start up. ComboBox
在启动时选择上述值。 Form_Load
, at any given time, if the user changes the value of the ComboBox
, have the change reflected in your class instance property. Form_Load
之后,在任何给定时间,如果用户更改了ComboBox
的值,则将更改反映在您的类实例属性中。 For that, I would do something like this: 为此,我将执行以下操作:
public partial class MainForm : Form
{
// Your class instance.
private SomeClass InstanceOfSomeClass = null;
public MainForm()
{
InitializeComponent();
// Initialize the RaBucket1Type property with Yellow.
InstanceOfSomeClass = new SomeClass(RaTypes.Yellow);
// Populating the ComboBox
comboBox1.DataSource = Enum.GetValues(typeof(RaTypes));
}
// At selected index changed event
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the selected value.
var selected = comboBox1.SelectedValue;
// Change the `RaBucket1Type` value of the class instance according to the user choice.
InstanceOfSomeClass.RaBucket1Type = (RaTypes)selected;
}
private void MainForm_Load(object sender, EventArgs e)
{
// At form load time, set the `SelectedItem` of the `ComboBox` to the value of `RaBucket1Type` of your class instance.
// Since we initialized it to `Yellow`, the `ComboBox` will show `Yellow` as the selected item at load time.
if (InstanceOfSomeClass != null)
{
comboBox1.SelectedItem = InstanceOfSomeClass.RaBucket1Type;
}
}
}
public enum RaTypes
{
Red,
Yellow
}
public class SomeClass
{
public RaTypes RaBucket1Type { get; set; }
public SomeClass(RaTypes raTypes) { RaBucket1Type = raTypes; }
}
Please do keep in mind this is a basic example to show you how to handle the situation and not a complete finished code. 请记住,这是一个基本示例,向您展示如何处理这种情况,而不是完整的完成代码。 You'll need to do a bunch of error checks to make sure class instances and selected items are not null etc.
您需要进行大量错误检查,以确保类实例和所选项不为null等。
I FOUND MY ANSWER: 我发现我的答案:
I had the SelectedIndexChanged event pointing to my event handler which means that when I "added" items to the ComboBox using: 我有SelectedIndexChanged事件指向我的事件处理程序,这意味着当我使用以下命令将项目“添加”到ComboBox时:
comboBox1.DataSource = Enum.GetValues(typeof(RaTypes));
it was triggering the event handler, and resetting my class property. 它触发了事件处理程序,并重置了我的class属性。 My event handler was this:
我的事件处理程序是这样的:
var selectedValue = cmbBucket1Type.SelectedValue;
So the simple solution was to: 因此,简单的解决方案是:
Add the following event handler in code AFTER I assign the DataSource 在分配数据源后,在代码中添加以下事件处理程序
bucketType1.SelectedIndexChanged += BucketTypeChanged; bucketType1.SelectedIndexChanged + = BucketTypeChanged;
This worked. 这工作了。
THANK YOU ALL FOR HELPING!! 谢谢大家的帮助!
-Ed -埃德
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.