I created my UserControl
like this:
MyUserCtrl myctrl = new MyUserCtrl() { DataContext = new MyViewModel()};
ControlCollection.Add(myctrl);
and i output it using this ItemsControl ItemsSource="{Binding ControlCollection}"
to the View.
It's clean and nice but the problem is I don't know how can I close those UserControls
that I opened.
And what if I just remove it to the collection. Thus the View Model will close too?
Do not assign a collection of UI elements to the ItemsSource of an ItemsControl. Instead, put the UI element in the ItemsControl's
ItemTemplate
and pass a collection of view model instances to the ItemsSource.Add a view model item to the collection property in your "main" view model:
To "close" a control, remove the appropriate item from the collection: