对象数据绑定c#winforms无法正常工作

时间:2015-08-29 15:41:00

标签: c# winforms object data-binding combobox

我已经看过很多关于数据库数据绑定的帖子,但是没有一个帖子有助于数据绑定到内存中的现有对象。我还查看了几个堆栈溢出帖子,其中人们说下面的代码应该绑定我的组合框上的属性:

        projectData = new ProjectData();

        this.parentTypeComboBox.DataSource = projectData.MobList;
        this.parentTypeComboBox.DisplayMember = "MobType";
        this.parentTypeComboBox.ValueMember = "MobType";

我的数据对象有各种属性的公共getter / setter,我已经在类上添加了INotifyPropertyChanged接口,但截至目前还没有将任何侦听器附加到该事件。从我所读过的内容来看,我应该做的就是让控件绑定到我的数据对象。知道为什么当我的对象列表发生变化时,我没有看到我的组合框被填充数据?

项目数据类:

public class ProjectData : INotifyPropertyChanged
{
    public static string PROJECT_OUTPUT_DIRECTORY = "..\\";
    private List<Mob> _mobList;

    public List<Mob> MobList
    {
        get { return _mobList; }
        set { _mobList = value; OnPropertyChanged("MobList"); }
    }

    public ProjectData()
    {
        MobList = new List<Mob>();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }      
}

Mob Class:

//Snippet mob of class
public partial class Mob : IEquatable<Mob>, INotifyPropertyChanged
{
    public Mob()
    {
        dataAttributeField = new List<MobDataAttribute>();
    }

    private List<MobDataAttribute> dataAttributeField;

    private string mobTypeField;

    private string parentTypeField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("DataAttribute")]
    public List<MobDataAttribute> DataAttribute
    {
        get
        {
            return this.dataAttributeField;
        }
        set
        {
            this.dataAttributeField = value;
            OnPropertyChanged("DataAttribute");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string MobType
    {
        get
        {
            return this.mobTypeField;
        }
        set
        {
            this.mobTypeField = value;
            OnPropertyChanged("MobType");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用此projectData = new ProjectData(); MobList是一个空列表。

如果您没有填充数据,则应将数据填充到列表中以在ComboBox中显示它。

请记住,每次填充数据时,都应更新ComboBox的DataSource属性:

this.comboBox1.DataSource = parent.Childs;

  

如果您绑定到未实现的数据源   IBindingList接口,例如ArrayList,绑定控件的数据   更新数据源时不会更新。例如,如果   你有一个绑定到ArrayList的组合框,并将数据添加到   ArrayList,这些新项目不会出现在组合框中。

以下是一个示例:

public partial class SampleForm : Form
{
    public SampleForm()
    {
        InitializeComponent();
    }

    private void SampleForm_Load(object sender, EventArgs e)
    {
        //Initialize parent and populate its Childs
        var parent = new Parent()
        {
            ParentName = "Parent 1",
            Childs = new List<Child>{
                new Child(){ChildName= "Child1"},
                new Child(){ChildName= "Child2"}
            }
        };

        this.comboBox1.DataSource = parent.Childs;
        this.comboBox1.DisplayMember = "ChildName";
        this.comboBox1.ValueMember = "ChildName";

    }

}

public class Parent
{
    public Parent()
    {
        Childs = new List<Child>();
    }
    public string ParentName { get; set; }
    public List<Child> Childs { get; set; }
}

public class Child
{
    public string ChildName { get; set; }
}

<强>截图:

enter image description here