I've seen a bunch of posts regarding databinding to databases but none of them have helped with databinding to an existing object in memory. I've also looked at several stack overflow posts where people have said the following code should've bound the properties on my combo box:
projectData = new ProjectData();
this.parentTypeComboBox.DataSource = projectData.MobList;
this.parentTypeComboBox.DisplayMember = "MobType";
this.parentTypeComboBox.ValueMember = "MobType";
My data object has public getters/setters for it's various properties and I've added the INotifyPropertyChanged interface on the classes but do not attach any listeners to the event as of now. From what I've read this should've been all I had to do to get the control to bind to my data object. Any idea why I'm not seeing my combo box get populated with data when my object list changes?
Project data class:
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");
}
}
}
void MobList_ListChanged(object sender, ListChangedEventArgs e) { this.parentTypeComboBox.DataSource = projectData.MobList; this.parentTypeComboBox.DisplayMember = "MobType"; this.parentTypeComboBox.ValueMember = "MobType"; }
– SonicD007