如何进行数据绑定文本框在一个列表℃的特定指数;>绑定、文本框、指数、数据

2023-09-04 01:07:45 作者:一身祖宗味儿

我有标准的数据绑定设置为我所有的文本框到一个对象是这样的:

I have standard databindings setup for all my TextBoxes to an Object like this:

TextBoxMenuID.DataBindings.Add("Text", _selectedObject, "ID");

和我想结合我的一些文本框到List<>的对象像这样在指数:

And I want to bind some of my TextBoxes to a List<> index within that Object like this:

TextBoxQ1.DataBindings.Add("Text", _selectedObject._qList[0].QuestionString, null);

绑定不工作这种方式。任何想法如何去这样的结合?

The binding isn't working this way. Any ideas how to go about this kind of binding?

谢谢, SleffTheRed

Thanks, SleffTheRed

推荐答案

感谢米蒂亚为code,但要改变索引只需要添加[]的:

Credit to Mitja for the code, but to change the index just add []'s:

list = new List<Person>();
list.Add(new Person { ID = 1, Name = "Name 1", Age = 21 });
list.Add(new Person { ID = 2, Name = "Name 2", Age = 28 });
list.Add(new Person { ID = 3, Name = "Name 3", Age = 44 });

textBox1.DataBindings.Add(new Binding("Text", list[0], "Name", false));
textBox2.DataBindings.Add(new Binding("Text", list[1], "Name", false));
textBox3.DataBindings.Add(new Binding("Text", list[2], "Name", false));

internal class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

 
精彩推荐
图片推荐