Good evening all,
Firstly thank you for the time taken to read this.
I'm having some difficulty with a sorted & grouped listbox in WPF vb.net 3.5 that has an items source bound to an ObservableCollection.
What I want to be able to do is to retrieve a piece of data from the ObservableCollection items source depending on what item in the listbox the user has selected.
I've almost got it working but because the listbox is sorted it does not match the index of the items source.
Here is the code that I have so far:
Dim i As Integer = lstBox1.Items.IndexOf(lstBox1.SelectedItem)
MessageBox.Show(myListSource.Item(i).Description.ToString, "Source Description")
As I previously mentioned, because the lstBox is sorted and also grouped the index's don't match up.
Does anyone know how I can get the correct information I want from the List Source depending on the selected item in the list box?
Again, thank you very much for your time,
Rob
Best Answer
You can bind the
ItemsSource
to theObservableCollection<Foo>
and bind theSelectedItem
to an instance ofFoo
.That way, you have removed the dependency on the index of the list - you can group and sort as you wish - by selecting an item in the list, the current instance in you backing class (probably a ViewModel) will update through the Binding.
It should look something like this
and in the code (ViewModel) acting as the DataContext for the view...
(with apologies if I have the vb syntax wrong)
So if you need to access the ListBox's
SelectedItem
in your ViewModel, you can just use theCurrentItem
property...