将ComboBox.Text绑定到对象属性

时间:2017-07-11 13:59:59

标签: c# winforms visual-studio data-binding

我在使用我的一个组合框找出问题的原因时遇到了一些麻烦。

我正在尝试将cboDropDownList.Text绑定到对象属性的值。该对象实现了INotifyPropertyChanged,并且我已经以编程方式将DataBinding添加到组合框中。以下是对象和组合框的代码:

对象:

public partial class ObjectType : object, INotifyPropertyChanged{

    private string objectIdField;

     public string objectId {
        get 
        {
            return this.objectIdField;
        }
        set 
        {
            this.objectIdField = value;
            this.RaisePropertyChanged("objectId");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

组合框:

this.cboDropDownList.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.ObjectBindingSource, "objectId", true));
this.cboDropDownList.DataSource = this.DataSetBindingSource;
this.cboDropDownList.DisplayMember = "item_id";
this.cboDropDownList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboDropDownList.FormattingEnabled = true;
this.cboDropDownList.Location = new System.Drawing.Point(177, 109);
this.cboDropDownList.Name = "cboDropDownList";
this.cboDropDownList.Size = new System.Drawing.Size(155, 21);
this.cboDropDownList.TabIndex = 27;
this.cboDropDownList.ValueMember = "item_id";

组合框的内容(可选项)已成功绑定到DataSet中的项目。但是,当加载组合框时,它始终显示相同的内容。例如,如果objectId的值为15,cboDropDownList将显示1,无论objectId的值如何,它都会显示1。 (1是DropDownList中的第一项)

有人会对为什么会这样做有任何想法吗?

谢谢!

0 个答案:

没有答案