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 selectedItemChanged 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With