简体   繁体   English

c#组合框绑定到对象列表

[英]c# combobox binding to list of objects

Is it possible to bind a ComboBox to a list of objects, but have the selectedvalue property point to the object, not a property of the object?是否可以将ComboBox绑定到对象列表,但 selectedvalue 属性指向对象,而不是对象的属性?

I only ask because we have some Business Objects which have references to other objects - such as a 'Year' object.我之所以这么问,是因为我们有一些业务对象引用了其他对象——例如“年份”对象。 That year object may need to be switched out for another year object.可能需要将那个年份对象切换为另一个年份对象。


Only solution I can come up with is to have another class with a single property, in this case pointing to a year object.我能想出的唯一解决方案是让另一个具有单个属性的类,在这种情况下指向一个 year 对象。 then bind the combobox to a List of these and set both the display and value members to the single property.然后将组合框绑定到这些列表,并将显示和值成员设置为单个属性。

But doing that for any 'lookups' we have seems like a bit of a pain??但是对于我们拥有的任何“查找”这样做似乎有点痛苦?

If you set the ValueMember to null the selected value will always be the object, not a property:如果您将 ValueMember 设置为 null,则所选值将始终是对象,而不是属性:

{
    public class TestObject
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }
    public partial class Form1 : Form
    {
        private System.Windows.Forms.ComboBox comboBox1;

        public Form1()
        {
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(23, 13);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 21);
            this.comboBox1.TabIndex = 0;
            this.comboBox1.SelectedValueChanged += new System.EventHandler(this.comboBox1_SelectedValueChanged);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.comboBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

            BindingList<TestObject> objects = new BindingList<TestObject>();
            for (int i = 0; i < 10; i++)
            {
                objects.Add(new TestObject() { Name = "Object " + i.ToString(), Value = i });
            }
            comboBox1.ValueMember = null;
            comboBox1.DisplayMember = "Name";
            comboBox1.DataSource = objects;
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue != null)
            {
                TestObject current = (TestObject)comboBox1.SelectedValue;
                MessageBox.Show(current.Value.ToString());
            }
        }
    }
}

You can bind a ComboBox to any list of values using the DataSource property.您可以使用DataSource属性将 ComboBox 绑定到任何值列表。 Or actually:或者实际上:

An object that implements the IList interface, such as a DataSet or an Array.实现 IList 接口的对象,例如 DataSet 或 Array。 The default is null.默认值为空。

You then use the ValueMember to control what you get from SelectedValue .然后使用ValueMember来控制从SelectedValue获得的内容。 Settings this to null as jmservera writes lets you get the object as it is in the DataSource .jmservera写入时将其设置为null可以让您获得DataSource的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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