ToolStripComboBox.SelectedItem更改不会传播到绑定源

时间:2010-04-28 12:00:39

标签: c# winforms data-binding toolstripcombobox

我的绑定设置如下:

        _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(请参阅我的代码注释),我的修复似乎没有做必要的工作。

1 个答案:

答案 0 :(得分:0)

你能改变吗?

 _selectXAxisUnitViewModelBindingSource.DataSource = typeof(SelectXAxisUnitViewModel);

 _selectXAxisUnitViewModelBindingSource.DataSource = new SelectXAxisUnitViewModel();