绑定文本框到comboBox.SelectedItem属性 [英] Binding textboxes to properties of a comboBox.SelectedItem

查看:1860
本文介绍了绑定文本框到comboBox.SelectedItem属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的WinForms和我有一个表示IQueryable的一个组合框。下面的组合框是一系列的,我想绑定到当前从组合框中选择文本框。

I'm using winforms and I've got a comboBox that represents an IQueryable. Below the combobox is a series of textboxes that I would like to be bound to the currently selected from the combo box.

下面是我的代码。

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"));
}



这样做绑定的一切,当我更改文本框中的值,它更新在组合框中初始选定的项目就好了。甚至当我更改说明,它更新在下拉别显示的文本,都认为是伟大的。

Doing this binds everything, When I change the values in the text boxes, it updates the initially selected item in the combo box just fine. Even when I change the description, it updates the displayed text in the drop don, all that is great.

然而,当我从下拉选择不同的项目下来,文本框不绑定到新选择的项目,他们保持绑定到旧的。

However, when I select a different item from the drop down, the text boxes don't bind to that newly selected item, they stay bound to the old one.

我是否需要删除,每一次重新添加我的绑定在组合框中选择改变?

Do I need to remove and re-add my bindings every time the selection changes on the combo box?

推荐答案

使用的 BindingSource的而不是直接依赖于L2S数据上下文。绑定源使用并发管理器来处理一切为您更新和L2S不

Use a BindingSource rather than directly relying upon the L2S data context. The binding source uses a concurrency manager to handle all the updating for you and the L2S does not

工作代码:

public partial class TestForm : Form
{
    public DataClassesDataContext DataContext;

    // Incorrect: public IQueryable<T> 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的距离的来源(想不出抗拒):

More about BindingSource from the source (couldnt resist):

BindingSource组件供应
多种用途。首先,它简化了
。通过
窗体上的控件绑定到数据提供货币管理,变更
通知等业务
之间的Windows窗体控件和
的数据源。这是由
使用
DataSource属性附加BindingSource组件
到数据源来实现的。对于复杂的
结合方案中,可以有选择
的成员属性设置为数据
源的
指定的列或列表。然后,将控件绑定到
的BindingSource。所有进一步的交互
相数据与
调用BindingSource组件来完成的。
有关示例对BindingSource的
如何简化绑定过程中,见
如何:绑定Windows窗体控件至
DBNull的数据库值和如何:
处理错误和例外
发生与数据绑定。数据源的导航和
更新为
通过诸如
的MoveNext,MoveLast,并删除完成。
这样的操作,如排序和
过滤通过排序
和筛选器属性处理。有关使用排序和BindingSource的
过滤的更多
信息,请参阅
如何:排序和筛选ADO.NET数据
与Windows窗体的BindingSource
组件。

The BindingSource component serves many purposes. First, it simplifies binding controls on a form to data by providing currency management, change notification, and other services between Windows Forms controls and data sources. This is accomplished by attaching the BindingSource component to your data source using the DataSource property. For complex binding scenarios you can optionally set the DataMember property to a specific column or list in the data source. You then bind controls to the BindingSource. All further interaction with the data is accomplished with calls to the BindingSource component. For examples on how the BindingSource can simplify the binding process, see How to: Bind Windows Forms Controls to DBNull Database Values and How to: Handle Errors and Exceptions that Occur with Databinding. Navigation and updating of the data source is accomplished through methods such as MoveNext, MoveLast, and Remove. Operations such as sorting and filtering are handled through the Sort and Filter properties. For more information on using sorting and filtering with the BindingSource, see How to: Sort and Filter ADO.NET Data with the Windows Forms BindingSource Component.

这篇关于绑定文本框到comboBox.SelectedItem属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆