开发者

DataBind listBox selected item to textboxes

开发者 https://www.devze.com 2022-12-13 09:46 出处:网络
I have ListBox data bind to list of users (collection): usersListBox.DataSource = null; usersListBox.DataSource = _users;

I have ListBox data bind to list of users (collection):

    usersListBox.DataSource = null;
    usersListBox.DataSource = _users;
    usersListBox.DisplayMember = "Name";
    usersListBox.ValueMember = "Id";

Now I want properties of selected item to be shown in different text boxes, so I do the binding:

nameTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Name");
loginTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Login");

When form load I can see that values of selected item appear in textboxes, but when selected item in listBox is changed, values in text boxes are still the same. Do I have to catch s开发者_如何学PythonelectedItemChanged of listbox and repeat binding of textboxes? Or I'm missing something and values in textboxes should change with changing selected item?


If someone needs answer: you have to create binding source object and assign it list box and textboxes:

 usersBindingSource = new BindingSource();
 usersBindingSource.DataSource = _presenter.Users;

 usersListBox.DataSource = usersBindingSource;
 usersListBox.DisplayMember = "Name";
 usersListBox.ValueMember = "Id";

 nameTextBox.DataBindings.Add("Text", usersBindingSource, "Name", true, DataSourceUpdateMode.OnPropertyChanged);
 loginTextBox.DataBindings.Add("Text", usersBindingSource, "Login", true, DataSourceUpdateMode.OnPropertyChanged);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号