views:

522

answers:

1

Hi!

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?

+1  A: 

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);
Adrian Serafin