开发者

Silverlight MVVM: MainView w/ SubViews OR MainViewModel w/SubViewModels

开发者 https://www.devze.com 2023-02-25 02:50 出处:网络
I have a MainView with a Tabbed UI. How do I add a new TabItem (= View) to the TabControl? Basicaly I see 2 ways:

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 mainVie开发者_Go百科w 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!


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消