我的绑定设置如下:
_selectXAxisUnitViewModelBindingSource = new BindingSource();
_selectXAxisUnitViewModelBindingSource.DataSource = typeof(SelectXAxisUnitViewModel);
_selectedUnitComboBoxBindingSource = new BindingSource();
_selectedUnitComboBoxBindingSource.DataSource = _selectXAxisUnitViewModelBindingSource;
_selectedUnitComboBoxBindingSource.DataMember = "AvailableUnits";
_selectedUnitComboBox.ComboBox.DataSource = _selectedUnitComboBoxBindingSource;
_selectedUnitComboBox.ComboBox.DisplayMember = String.Empty;
_selectedUnitComboBox.ComboBox.ValueMember = String.Empty;
_selectedUnitComboBox.ComboBox.DataBindings.Add("SelectedItem",
_selectXAxisUnitViewModelBindingSource,
"SelectedUnit", true, DataSourceUpdateMode.OnPropertyChanged);
// this is a bug in the .Net framework: http://connect.microsoft.com/VisualStudio/feedback/details/473777/toolstripcombobox-nested-on-toolstripdropdownbutton-not-getting-bindingcontext
_selectedUnitComboBox.ComboBox.BindingContext = this.BindingContext;
属性“AvailableUnits”是字符串的集合,“SelectedUnit”是字符串属性。现在下拉列表按预期填充,但是当我选择列表中的项目时,更改不会传播到绑定源。知道我做错了什么吗?
更新:
我创建了一个小测试项目,当我将 ToolStripComboBox 添加为另一个 ToolStripItem 的子项时会出现此问题。如果我将 ToolStripItem 直接添加到 MenuStrip 一切正常。BindingContext 在添加为子项时未分配给 ToolStripComboBox(请参阅我的代码注释),并且我的修复似乎没有做任何必要的事情来使它工作。