0

I want to bind an ObservableCollection of model items to an ItemsControl and be able to manage adding and removing items to and from that collection, as well as synchronize presence of member in the collection with views of user controls that represent those items. but I realized that when I trying to remove the models, they are still there and the views are still displayed as the last time I added one.

I think my problem is that I can't understand how to manage the ViewModel and View that associated with the model that I trying to remove. do I also have to dereference the model from the ViewModel(or the ViewModel from the View) to let the GC do its work? do I need to construct the ViewModel or Models in code-behind? or is it enough to declare the DataContext's type in the XAML, or if both are there it is redundant? (maybe some null reference caused this because there was no reference to remove?)

I'm new to WPF and MVVM and I think that question is pretty basic, but I didn't find any question about that integration of ItemsControl with ObservableCollection.

  • Create a view model class with a public property like `public ObservableCollection MyItems { get; set; }`. Assign an instance of the view model class to the `DataContext` property of your MainWindow. Then bind the ItemsControl's ItemsSource property like `ItemsSource="{Binding MyItems}"`. – Clemens Sep 09 '17 at 18:19
  • @Clemens Shouldn't you answer that in an answer and no in a comment? anyway, I found similar answer [here](https://stackoverflow.com/questions/964751/should-my-viewmodel-have-an-observablecollection-of-views-or-viewmodels?rq=1) do the collection's members have to be view models? – Yohai Pouny Sep 09 '17 at 21:26

0 Answers0