【发布时间】:2015-11-12 12:40:22
【问题描述】:
我有一个通过数据源绑定到列表的组合框。出于某种原因,当数据源项更改时,组合框中的项似乎不会自动更新。我可以在调试器中看到数据源包含正确的项目。
StackOverflow 上有很多关于此的答案,但大多数都没有答案,不适合我,或者需要从使用列表更改为 BindingLists,由于使用方法 BindingLists 的代码量很大,我无法执行此实例没有。
肯定有一种简单的方法来告诉 ComboBox 刷新它的项目吗?我不敢相信这不存在。我已经有一个在需要更新 Combo 时触发的事件,但我更新值的代码无效。
组合声明:
this.devicePortCombo.DataBindings.Add(
new System.Windows.Forms.Binding("SelectedValue",
this.deviceManagementModelBindingSource, "SelectedDevice", true,
DataSourceUpdateMode.OnPropertyChanged));
this.devicePortCombo.DataSource = this.availableDevicesBindingSource;
更新组合框的代码:
private void Instance_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "AvailableDevices")
{
// Rebind dropdown when available device list changes.
this.Invoke((MethodInvoker)delegate
{
devicePortCombo.DataSource = AvailableDevicesList;
devicePortCombo.DataBindings[0].ReadValue();
devicePortCombo.Refresh();
});
}
}
【问题讨论】:
-
如果您使用的是 BindingSource,您是否尝试过 BindingSource.ResetBindings() msdn.microsoft.com/en-us/library/…
-
当我在必须将数据源设置为空之前遇到此问题时,然后重新添加它。
-
但这可能会导致任何选定的值变为未设置,这与使用 BindingList 时的工作方式不同。