I'm thinking if you could check these points:
- Did you use generic type for your
ObservableCollection
? Could you please provide yourTeacher
class andDepartment
class? Also could you please provide the code of XAML file and how you set up the data-binding? - Ideally, the
ObservableCollection
should be initialized as an empty list when you create the instance of the ViewModel. Then you just add or remove elements from the collection. - Line 12 makes no sense. Not sure why you created a new collection but it is not used after that.
- If you already set the data-binding of the ListView, you could just operate the
ObservableCollection
, not assign theItemsSource
directly.
My suggestion is:
Use MVVM pattern and set up the data-binding for ListView controls. You could bind the properties such as ItemsSource
and SelectedItem
of the ListView. ObservableCollection
is for the ItemsSource
of the ListView. For SelectedItem
, you should have other properties called SelectedDepartment
and SelectedTeacher
that implement INotifyPropertyChanged
interface. When you remove items from ListViews, just update the ObservableCollection
, SelectedDepartment
and SelectedTeacher
. The UI would be updated automatically.