【问题标题】:Binding textboxes to properties of a comboBox.SelectedItem将文本框绑定到 comboBox.SelectedItem 的属性
【发布时间】: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


    【解决方案1】:

    我最初的答案是错误的,诚然我并不完全理解这里发生的一切,但我有一个可行的解决方案。

    基本上,您需要从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();
        }
    }
    

    这个MSDN article帮了很多忙。

    【讨论】:

    • 我收到此异常“在 DataSource 上找不到 DataMember 属性 'SelectedItem'。”
    • 我还尝试绑定到一个成员变量并在下拉菜单中设置 onchange,我将该变量重新分配给当前选定的变量。这也导致了我最初的问题。所以,我不认为这是我需要的解决方案
    • 希望这些更改能帮助您入门。我真的还没有完全理解它,但这是我通过使用它并使用链接的 MSDN 文章想出的。
    • 太棒了,我肯定会在星期一跟进,现在+1,再次感谢
    • 好的,所以我学会了添加数据源并让设计人员为您连接所有数据绑定。我会贴一些设计师为你写的代码。
    【解决方案2】:

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

    工作代码:

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

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

    BindingSource 组件服务于 许多目的。首先,它简化了 将表单上的控件绑定到数据 提供货币管理、找零 通知和其他服务 Windows 窗体控件和 数据源。这是通过 附加 BindingSource 组件 使用 数据源属性。对于复杂的 您可以选择绑定场景 将 DataMember 属性设置为 数据中的特定列或列表 资源。然后将控件绑定到 绑定源。所有进一步的互动 与数据完成 调用 BindingSource 组件。 有关如何使用 BindingSource 的示例 可以简化绑定过程,见 如何:将 Windows 窗体控件绑定到 DBNull 数据库值以及如何: 处理错误和异常 与数据绑定一起发生。导航和 数据源的更新是 通过方法如 MoveNext、MoveLast 和 Remove。 排序等操作 过滤是通过排序处理的 和过滤器属性。更多 有关使用排序和 使用 BindingSource 进行过滤,请参阅 如何:对 ADO.NET 数据进行排序和筛选 使用 Windows 窗体 BindingSource 组件。

    【讨论】:

      猜你喜欢
      • 2012-07-19
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 2011-05-17
      相关资源
      最近更新 更多