简体   繁体   English

尝试将组合框绑定到C#中的枚举

[英]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: 有人可以帮助我了解:

  1. How to set the combobox to current value, and 如何将组合框设置为当前值,以及
  2. How to handle the event handler so I can set the property to whatever was selected? 如何处理事件处理程序,以便可以将属性设置为所选的内容?

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: 通常,术语AddPopulateSelect用来表示以下内容:

  • Add - Add an item to the existing set of items in the combo box. 添加-将项目添加到组合框中的现有项目集中。
  • Populate - Initialize the combo box with a set of items. 填充-用一组项目初始化组合框。
  • Select (Display) - Choose one among many items in the combo box as the selected item. 选择(显示)-在组合框中的许多项目中选择一项作为所选项目。 Normally this item will be displayed in the combo box visible area. 通常,此项目将显示在组合框可见区域中。

Having cleared that up, I assume following is what you want to do. 清除这些内容后,我假设您要执行以下操作。

  1. Initially populate the ComboBox with a set of values. 最初使用一组值填充 ComboBox In your case, values of RaType Enum . 您的情况是RaType Enum值。
  2. Create an instance of your class which contains the property mentioned. 创建您的类的实例,其中包含提到的属性。 Since you didn't name that class I'll simply name it SomeClass . 由于您没有命名该类,因此我将其简单命名为SomeClass
  3. Initialize the RaBucket1Type property of the said class instance with an enum value of your choice. 使用您选择的enum值初始化所述类实例的RaBucket1Type属性。 I'll initialize it to Yellow . 我将其初始化为Yellow
  4. Have the ComboBox select the said value at start up. ComboBox在启动时选择上述值。
  5. After 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: 因此,简单的解决方案是:

  1. Remove the hard-coded event handler from the Visual Studio GUI. 从Visual Studio GUI中删除硬编码的事件处理程序。
  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM