0
votes

I have a MainView with a Tabbed UI.

How do I add a new TabItem (= View) to the TabControl? Basicaly I see 2 ways:

1.)
* from code in the MainView I can add a new tab.
* the new tab contains a view with a referece to it's viewmodel.

2.)
* from code in the MainViewModel I can add a new viewmodel to a List of childViewModels
* the tabcontrol of the mainView is bound to that list

I prefere case #1 somehow, cause I think the view should know and instanciate it's VM (maybe by using the MVVM light ViewModelLocator) and not the other way round.
But how can I refere from the newly created VM to the MainVM? For example: the MainVM has a property 'IsAdmin'; how can I access (bind) that property from the SubViewModel?

alternative #2: how does the TabControl know, which view should be "rendered" for different ViewModels? How can I "map" from the SubViewModels to the corresponding "SubViews"?

Thanks for sharing your ideas!

2
what is the nature of the views? (are they the same), and how are they added? - AbdouMoumen
The views can be very different. Like visualization of entities (products, customers) but also previews of documents, etc. Currently I add them from a codebehind method of the surrounding MainView and let the TabControl itself do the handling of closing and removing tabs, which works quite well. But I do believe, that this isn't the best approach and the "Tab-Controlling" should go somewhere else... - Thomas Mutzl

2 Answers

0
votes

I would check out this SO post as the answer can be applied to helping you with your problem.

In the spirit of MVVM, you will want to follow alternative #2. Let your ViewModel logic help you determine which "tabs" you need to display and use DataTemplates to represent those objects. Then you will get them bound to the DataContext of the View and your binding in the DataTemplate (View) will work correctly.

0
votes

Thomas,

MVVM really is MVVMC. I would advise having a controller for the MainView which contains a method for creating a new tab. If the TabControl is complicated, you might put the functionality in the TabControl itself.

Separation of concerns (MODEL versus VIEWMODEL versus VIEW versus CONTROLLER) is compromised when actuation functionality is located in the models. (M or VM).

Regards,

Guido