It is understood that if you wish to get a container from an item in a ListBox you can use:
ListBoxItem lbi1 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(0));
(Source: http://bea.stollnitz.com/blog/?p=7)
Assuming the above listbox was bound to a List, how can I do the opposite? As in, what if I wanted to get the databound data in the list by using the container (lbi1 in the above example)?
Ok I think I have found the answer 🙂
For ListBox items or other similar ContentControls the data can be retrieved by something like:
For other controls, like TreeViewItems, as they are not ContentControls, the data can be retrieved by accessing the DataContext: