The following code works as you’d expect — MyProperty on the model is updated when the user picks a new item in the dropdown.
comboBox1.DataBindings.Add("SelectedValue", myModel, "MyProperty", true,
DataSourceUpdateMode.OnPropertyChanged);
The following, however, doesn’t work the same way and the model update isn’t triggered until the input focus moves to another control on the form:
comboBox1.DataBindings.Add("SelectedItem", myModel, "MyProperty", true,
DataSourceUpdateMode.OnPropertyChanged);
Does anybody know why? I don’t even know where to start investigating the cause. Pointers in the right direction to start the investigation or an outright explanation would be equally appreciated.
Aside: for my purposes, I ended up binding to both SelectedItem and SelectedValue. This way I get instant model updates based on UI changes (through the SelectedValue binding), and UI updates based on programmatic model changes (through the SelectedItem binding).
The
ComboBoxcontrol inherits from theListControlcontrol.The
SelectedItemproperty is a proper member of theComboBoxcontrol. The event that is fired on change isComboBox.SelectionChangeCommittedComboBox.SelectionChangeCommitted
The
SelectedValueproperty is inherited from theListControlcontrol.As such, this property will fire the
ListControl.SelectedValueChangedevent.ListControl.SelectedValueChanged
That said, they won’t fire the
INotifyPropertyChanged.PropertyChangedevent the same, but they will anyway. The only difference is in the firing event.SelectedValueChangedis fired as soon as a new selection is made from the list part of the ComboBox, andSelectedItemChangedis fired when the item is displayed in the TextBox portion of the ComboBox.In short, they both represent something in the list part of the ComboBox. So, when binding either property, the result is the same, since the
PropertyChangedevent is fired in either case. And since they both represent an element from the list, the they are probably treated the same.Does this help?
EDIT #1
Assuming that the list part of the ComboBox represents a property (as I can’t confirm since I didn’t write the control), binding either of
SelectedItemorSelectedValueaffects the same collection inside the control. Then, when this property is changed, the same occurs in the end. TheINotifyPropertryPropertyChanged.PropertyChangedevent is fired on the same property.