gpt4 book ai didi

c# - 对象数据绑定(bind) c# winforms 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 13:01:01 25 4
gpt4 key购买 nike

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

        projectData = new ProjectData();

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

我的数据对象具有各种属性的公共(public) getter/setter,我已经在类上添加了 INotifyPropertyChanged 接口(interface),但目前还没有将任何监听器附加到事件。从我读过的内容来看,这应该是我必须做的让控件绑定(bind)到我的数据对象。知道为什么当我的对象列表更改时我没有看到我的组合框填充数据吗?

项目数据类:

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

暴民等级:

//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");
}
}
}

最佳答案

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

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

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

this.comboBox1.DataSource = parent.Childs;

If you are bound to a data source that does not implement the IBindingList interface, such as an ArrayList, the bound control's data will not be updated when the data source is updated. For example, if you have a combo box bound to an ArrayList and data is added to the ArrayList, these new items will not appear in the combo box.

这是一个示例:

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

关于c# - 对象数据绑定(bind) c# winforms 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32287782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com