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

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

问题描述

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



这是我的代码。

  public partial class TestForm:Form 
{
public DataClassesDataContext DataContext;

public IQueryable< T>数据源;

// Ctor
public TestForm()
{
InitializeComponent();

// L2S数据上下文
this.DataContext = new DataClassesDataContext();

//获取数据源的变量
this.datasource = this.DataContext.Ts;

//设置组合框的绑定
this.comboBox1.DataSource = this.datasource;
this.comboBox1.DisplayMember =说明;
this.comboBox1.ValueMember =Id;

//将文本框的数据绑定分配给组合框的selectedItem
//这是问题所在,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中显示的文本,这一切都很棒。



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



我需要删除并重新添加我的绑定每次组合框上的选择更改?

解决方案

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



工作代码:

  public partial class TestForm:Form 
{
public DataClassesDataContext DataContext;

//不正确:public IQueryable< T>数据源;
//正确:
public BindingSource TsDataSource;

// Ctor
public TestForm()
{
InitializeComponent();

// L2S数据上下文
this.DataContext = new DataClassesDataContext();

//获取数据源的变量
//不正确:this.datasource = this.DataContext.Ts;
//正确:
this.TsDataSource = new BindingSource();
this.TsDataSource.DataSource = this.DataContext.Ts;

//设置组合框的绑定
this.comboBox1.DataSource = this.TsDataSource;
this.comboBox1.DisplayMember =说明;
this.comboBox1.ValueMember =Id;

//将文本框的数据绑定分配给组合框的selectedItem
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从(无法抗拒):



< blockquote>

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



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.

Here is my code.

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?

解决方案

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

Working code:

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

More about BindingSource from the source (couldnt resist):

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天全站免登陆