将文本框绑定到comboBox.SelectedItem的属性

我正在使用winforms,我有一个代表IQueryable的comboBox。 combobox下方是一系列文本框,我希望将其绑定到当前从combobox中选择的文本框。

这是我的代码。

public partial class TestForm : Form { public DataClassesDataContext DataContext; public IQueryable datasource; // Ctor public TestForm() { InitializeComponent(); // L2S data context this.DataContext = new DataClassesDataContext(); // Get the variable for the data source this.datasource = this.DataContext.Ts; // setup the binding for the combobox this.comboBox1.DataSource = this.datasource; this.comboBox1.DisplayMember = "Description"; this.comboBox1.ValueMember = "Id"; // assign the databindings of the text boxes to the selectedItem of the combo box // this is where the problem is, afaik TId.DataBindings.Add(new Binding("Text", this.comboBox1.SelectedItem, "Id")); TUser.DataBindings.Add(new Binding("Text", this.comboBox1.SelectedItem, "User")); TDescription.DataBindings.Add(new Binding("Text", this.comboBox1.SelectedItem, "Description")); } 

这样做会绑定所有内容,当我更改文本框中的值时,它会更新combobox中最初选择的项目。 即使我更改了描述,它也会更新drop don中显示的文本,这一切都很棒。

但是,当我从下拉列表中选择不同的项目时,文本框不会绑定到新选择的项目,它们将保持与旧项目绑定。

每次选择更改combobox时,是否需要删除并重新添加我的绑定?

我原来的答案是错误的,并且我承认我并不完全理解这里发生了什么,但我有一个有效的解决方案。

基本上你需要从BindingContext获取BindingManagerBase并使用它来对每个SelectedItemChanged事件强制执行数据绑定。

 public partial class TestForm : Form { public DataClassesDataContext DataContext; public IQueryable datasource; private BindingManagerBase bmComboBoxSelectedItem; // Ctor public TestForm() { InitializeComponent(); // L2S data context this.DataContext = new DataClassesDataContext(); // Get the variable for the data source this.datasource = this.DataContext.Ts; // setup the binding for the combobox this.comboBox1.DataSource = this.datasource; this.comboBox1.DisplayMember = "Description"; this.comboBox1.ValueMember = "Id"; // assign the databindings of the text boxes to the selectedItem of the combo box // this is where the problem is, afaik TId.DataBindings.Add(new Binding("Text", this.comboBox1, "SelectedItem.Id")); TUser.DataBindings.Add(new Binding("Text", this.comboBox1, "SelectedItem.User")); TDescription.DataBindings.Add(new Binding("Text", this.comboBox1, "SelectedItem.Description")); bmComboBoxSelectedItem = this.BindingContext[this.comboBox1, "SelectedItem"]; } // make sure you assign this event on the forms designer or your preferred method private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { bmCustomers.ResumeBinding(); } } 

这篇MSDN文章帮了很多忙。

使用BindingSource而不是直接依赖于L2S数据上下文。 绑定源使用并发管理器来处理所有更新,而L2S则不会

工作代码:

 public partial class TestForm : Form { public DataClassesDataContext DataContext; // Incorrect: public IQueryable datasource; // Correct: public BindingSource TsDataSource; // Ctor public TestForm() { InitializeComponent(); // L2S data context this.DataContext = new DataClassesDataContext(); // Get the variable for the data source // Incorrect: this.datasource = this.DataContext.Ts; // Correct: this.TsDataSource = new BindingSource(); this.TsDataSource.DataSource = this.DataContext.Ts; // setup the binding for the combobox this.comboBox1.DataSource = this.TsDataSource; this.comboBox1.DisplayMember = "Description"; this.comboBox1.ValueMember = "Id"; // assign the databindings of the text boxes to the selectedItem of the combo box TId.DataBindings.Add(new Binding("Text", this.TsDataSource, "Id")); TUser.DataBindings.Add(new Binding("Text", this.TsDataSource, "User")); TDescription.DataBindings.Add(new Binding("Text", this.TsDataSource, "Description")); } 

更多关于来自源的 BindingSource(无法抗拒):

BindingSource组件有很多用途。 首先,它通过在Windows窗体控件和数据源之间提供货币管理,更改通知和其他服务,简化了表单上对数据的绑定控制。 这是通过使用DataSource属性将BindingSource组件附加到数据源来实现的。 对于复杂绑定方案,您可以选择将DataMember属性设置为数据源中的特定列或列表。 然后,您将控件绑定到BindingSource。 通过调用BindingSource组件完成与数据的所有进一步交互。 有关BindingSource如何简化绑定过程的示例,请参见如何:将Windows窗体控件绑定到DBNull数据库值以及如何:处理数据绑定时出现的错误和exception。 通过MoveNext,MoveLast和Remove等方法完成数据源的导航和更新。 排序和过滤等操作通过“排序”和“过滤”属性进行处理。 有关使用BindingSource进行排序和筛选的更多信息,请参见如何:使用Windows窗体绑定源组件对ADO.NET数据进行排序和筛选。