简体   繁体   English

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

[英]Binding textboxes to properties of a comboBox.SelectedItem

I'm using winforms and I've got a comboBox that represents an IQueryable. 我正在使用winforms,并且有一个表示IQueryable的comboBox。 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? 每当组合框上的选择更改时,是否需要删除并重新添加绑定?

My original answer was wrong, and admittedly I do not fully understand what all is happening here, but I have a solution which is working. 我最初的回答是错误的,并且诚然我不完全了解这里发生的一切,但是我有一个可行的解决方案。

Basically you need to grab the BindingManagerBase from the BindingContext and use it to enforce databinding on each SelectedItemChanged event. 基本上,您需要从BindingContext获取BindingManagerBase并将其用于对每个SelectedItemChanged事件强制执行数据绑定。

public partial class TestForm : Form
{
    public DataClassesDataContext DataContext;

    public IQueryable<T> 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();
    }
}

This MSDN article helped a lot. 这篇MSDN文章很有帮助。

Use a BindingSource rather than directly relying upon the L2S data context. 使用BindingSource而不是直接依赖L2S数据上下文。 The binding source uses a concurrency manager to handle all the updating for you and the L2S does not 绑定源使用并发管理器为您处理所有更新,而L2S不会

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): 更多关于BindingSource的信息 (无法抗拒):

The BindingSource component serves many purposes. BindingSource组件有许多用途。 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. 首先,它通过在Windows窗体控件和数据源之间提供货币管理,更改通知和其他服务,简化了窗体上与数据的绑定控件。 This is accomplished by attaching the BindingSource component to your data source using the DataSource property. 这是通过使用DataSource属性将BindingSource组件附加到数据源来完成的。 For complex binding scenarios you can optionally set the DataMember property to a specific column or list in the data source. 对于复杂的绑定方案,您可以选择将DataMember属性设置为数据源中的特定列或列表。 You then bind controls to the BindingSource. 然后,将控件绑定到BindingSource。 All further interaction with the data is accomplished with calls to the BindingSource component. 与数据的所有进一步交互都是通过对BindingSource组件的调用来完成的。 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. 有关BindingSource如何简化绑定过程的示例,请参见如何:将Windows窗体控件绑定到DBNull数据库值和如何:处理数据绑定中发生的错误和异常。 Navigation and updating of the data source is accomplished through methods such as MoveNext, MoveLast, and Remove. 数据源的导航和更新是通过诸如MoveNext,MoveLast和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. 有关对BindingSource使用排序和筛选的更多信息,请参见如何:使用Windows窗体BindingSource组件对ADO.NET数据进行排序和筛选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM