简体   繁体   English

用户控件内的绑定控件

[英]binding controls inside a user control

I have a user control that contains several text boxes bound to a bindingSource. 我有一个用户控件,其中包含绑定到bindingSource的几个文本框。 I want to drop that user control onto another form and pass the datasource to the control to use. 我想将该用户控件放到另一个表单上,并将数据源传递给控件使用。

I have tried making the user control's bindingSource public and setting it to the same bindingSource on the form, 我已经尝试将用户控件的bindingSource公开并将其设置为表单上的相同bindingSource,

    List<Person> data = MyGetData(); // returns a list of people
    this.formControl.bindingSource.DataSource = data;   // where formControl is my user control containing bound text boxes.

inside my user control I have a textbox called textboxFirstName, and a bindingsource called bindingSource 在我的用户控件中,我有一个名为textboxFirstName的文本框,以及一个名为bindingSource的bindingsource

Using the designer, I set bindingSource.DataSource to be Person where Person is one of my domain classes. 使用设计器,我将bindingSource.DataSource设置为Person,其中Person是我的域类之一。

Also using the designer I set textboxFirstName.DataBindings.Text to "bindingSource - FirstName" by picking it from the list of Person properties. 同样使用设计器我将textboxFirstName.DataBindings.Text设置为“bindingSource - FirstName”,方法是从Person属性列表中选择它。

There is no error message, but the text box is not displaying the data. 没有错误消息,但文本框未显示数据。

[Update] I can get the binding working if I create the following procedure and call it. [更新]如果我创建以下过程并调用它,我可以使绑定工作。

    public void SetBinding(BindingSource bs)
    {
        this.bindingSource = bs;
        this.firstNameTextBox.DataBindings.Clear();
        this.firstNameTextBox.DataBindings.Add(new Binding("Text", bs, "firstName"));
    }

However I am wondering why setting the binding source alone is not enough. 但是我想知道为什么单独设置绑定源是不够的。

Note at design time I had set the user control's bindingSource DataSource to People. 请注意,在设计时我已将用户控件的bindingSource DataSource设置为People。 This enabled me to pick the text box's DataBindings.Text property from a list. 这使我能够从列表中选择文本框的DataBindings.Text属性。

You first 2 lines and setting the binding from the designer should work. 你前2行并设置设计师的绑定应该工作。 The issues is that your dataSource is a collection and you are trying to bind it to a textbox. 问题是您的dataSource是一个集合,并且您正在尝试将其绑定到文本框。 This method only works for controls that hold a collection (datagridview, listbox, etc.). 此方法仅适用于包含集合的控件(datagridview,listbox等)。 But in your case, in order to bind a textBox to a certain property you need the dataSource to be one Person object that has that property. 但在您的情况下,为了将textBox绑定到某个属性,您需要将dataSource作为具有该属性的一个 Person对象。

try this line: 试试这一行:

this.formControl.bindingSource.DataSource = data.First();

and see if your text is displayed. 并查看您的文本是否显示。 If you want to display the entire collection than you need to create one textBox for every object in the collection or change the control type. 如果要显示整个集合,而不是为集合中的每个对象创建一个textBox或更改控件类型。

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

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