Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does BindingList<T> know the selected Item?

When you use a BindingList as Binding for lets say a ListBox

listBox.DataSource = myBindingList;

and bind some labels to the same source:

labelName.DataBindings.Add("Text",
                           myBindingList,
                           "Name",
                           false,
                           DataSourceUpdateMode.OnPropertyChanged);

The value of the labels change when the SelectedItem of my listBox is changed.

Why do the values of my labels always refer the currently selected item?

How do they know, by just giving them the BindingList as source, which item in the List is the selected one?

like image 966
Pulle Avatar asked Oct 29 '22 22:10

Pulle


1 Answers

When doing data-binding in windows-forms, the UI has access to a BindingContext and a CurrencyManager. The BindingContext comes (ultimately) from the parent form, and the CurrencyManager is obtained from the BindingContext via the data source reference. So: if two controls on the same form are given the same data-source, then assuming they use the normal binding patterns, they will get the same CurrencyManager. It is the CurrencyManager that tracks the selected item.

Note: it is possible to split the BindingContext on a per-control basis; but the default is for it to be inherited - thus all controls on the same form will share a BindingContext.

like image 104
Marc Gravell Avatar answered Nov 15 '22 05:11

Marc Gravell