C#组合框绑定到的对象的列表 [英] c# combobox binding to list of objects

查看:150
本文介绍了C#组合框绑定到的对象的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速的问题,是有可能绑定一个组合框对象的名单,但有SelectedValue属性点的对象,而不是对象的属性。

Quick question, 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.

我只问,因为我们有对其他对象的引用一些业务对象 - 如年份的对象。那年对象可能需要一年的对象进行切换出来。

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. 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??

马龙的

推荐答案

如果您设置ValueMember为null选择的值永远是对象,而不是一个属性:

If you set the ValueMember to null the selected value will always be the object, not a property:

{
    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());
            }
        }
    }
}

这篇关于C#组合框绑定到的对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆