I have an ObservableCollection of an object in C#. This object implements an interface. I would like to be able to convert the ObservableCollection of the object to an ObservableCollection of the interface without having to parse through the collection.
So for example:
I have an object “Guitar” which implements an interface called “IMusicalInstrument”. I bind an ObservableCollection of Guitar objects to MyListBox. I want the following line of code to be able to convert listbox itemsSource to an ObservableCollection.
ObservableCollection<IMusicalInstrument> InstrumentList =
(ObservableCollection<IMusicalInstrument>)MyListBox.ItemsSource;
Currently, that is giving me an InvalidCastException.
Is there a way to accomplish this (without having to parse through the collection)?
Thanks,
Seth
Unless
MyListBox.ItemsSourceis an instance ofObservableCollection<IMusicalInstrument>, then no, you cannot cast it.