代码之家  ›  专栏  ›  技术社区  ›  Allen Rice 0x6A75616E

将文本框绑定到组合框的属性。SelectedItem

  •  0
  • Allen Rice 0x6A75616E  · 技术社区  · 16 年前

    这是我的密码。

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

    但是,当我从下拉列表中选择不同的项目时,文本框不会绑定到新选择的项目,而是绑定到旧项目。

    2 回复  |  直到 16 年前
        1
  •  1
  •   Nick Larsen    16 年前

    我最初的回答是错误的,诚然,我并不完全理解这里发生的一切,但我有一个解决办法,它正在发挥作用。

    基本上你需要抓住 BindingManagerBase BindingContext 并使用它在每个服务器上强制执行数据绑定 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 帮助很大。

        2
  •  0
  •   Allen Rice 0x6A75616E    16 年前

    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 (忍不住):

    通过将窗体上的控件绑定到数据 通知和其他服务 数据来源。这是通过 使用 您可以选择绑定场景 将DataMember属性设置为 数据中的特定列或列表 来源。然后将控件绑定到 数据是用 调用BindingSource组件。 有关BindingSource 可以简化绑定过程,请参见 如何:将Windows窗体控件绑定到 DBNull数据库值以及如何: 与数据绑定一起发生。导航和 正在更新数据源 通过以下方法完成: 排序和排序等操作 使用BindingSource进行筛选,请参阅 如何:对ADO.NET数据进行排序和筛选 使用Windows窗体绑定源 组成部分。