I am trying to bind a tool strip combo box to a data source but it is not working as expected.
Data source:Code:class MapCellType : ModelBase { private int m_value; private Color m_color; private string m_name; public MapCellType() { } public int CellValue { get { return m_value; } set { m_value = value; OnPropertyChanged("CellValue"); } } public Color Color { get { return m_color; } set { m_color = value; OnPropertyChanged("Color"); } } public string CellName { get { return m_name; } set { m_name = value; OnPropertyChanged("CellName"); } } public void OnColorClicked(object sender,EventArgs e) { ColorDialog dlg = new ColorDialog(); if (dlg.ShowDialog() == DialogResult.OK) { Color = dlg.Color; } } }
Binding code:Code:private BindingList<MapCellType> m_mapCellTypes = new BindingList<MapCellType>();
Property in main form:Code:BindingSource source = new BindingSource(); source.Add(m_mapCellTypes); tscboObjects.ComboBox.DataSource = source; tscboObjects.ComboBox.DisplayMember = "CellName"; tscboObjects.ComboBox.ValueMember = "CellValue"; tscboObjects.ComboBox.DataBindings.Add("SelectedItem", this, "MapValue",true,DataSourceUpdateMode.OnPropertyChanged); tscboObjects.ComboBox.BindingContext = this.BindingContext;
ModelBase is a simple class that implements INotifyPropertyChanged and provides a method to fire this event.Code:public int MapValue { get { return m_mapValue; } set { m_mapValue = value; usrMapEditor1.MapValue = m_mapValue; usrMapEditor1.CellColor = m_mapCellTypes[tscboObjects.SelectedIndex].Color; } }
The combo box does not show the correct display values. It show what is specified in the value member which is not correct. Second the combo box is not being bound to the MapValue property. When the value changes in the combo box no breakpoints are hit within the setter of the MapValue property.
Anyone else encountered this with tool strip combo boxes?



LinkBack URL
About LinkBacks


