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?
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
.
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