【发布时间】:2011-01-13 15:16:27
【问题描述】:
我正在使用 winforms,并且我有一个代表 IQueryable 的组合框。组合框下方是一系列文本框,我想将它们绑定到当前从组合框中选择的文本框。
这是我的代码。
public partial class TestForm : Form
{
public DataClassesDataContext DataContext;
public IQueryable<T> 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"));
}
这样做会绑定所有内容,当我更改文本框中的值时,它会很好地更新组合框中最初选择的项目。即使我更改了描述,它也会更新 drop don 中显示的文本,这一切都很棒。
但是,当我从下拉列表中选择其他项目时,文本框不会绑定到新选择的项目,它们会保持绑定到旧项目。
每次组合框上的选择更改时,我是否需要删除并重新添加我的绑定?
【问题讨论】:
标签: c# winforms linq-to-sql data-binding textbox