Reputation: 37184
I have an entry-point View with a tab control. Each tab is going to have a user control embedded within it. Each embedded view inherits from the same base class and will need to be updated as a key field on the entry-point view is updated.
I'm thinking the easiest way to design this page is to have the entry-point ViewModel create and expose a collection of views so the entry-point View can just bind to the user control elements using a DataTemplate on the tab control.
Is it okay for a ViewModel to instantiate and provide UI elements for its View?
Upvotes: 2
Views: 115
Reputation: 19077
This is a difficult question, but most of the MVVM people consider it a code smell.
The ViewModel should not care about UI implementation details. (Separation of Concerns)
It is simply out of its scope.
I know, sometimes it is hard to do it otherwise. (Especially with controls wih non-bindable properties such as the RichTextBox's Document property.)
If you shared some more details about your idea, I could go into more detail, but here is what I think:
What stops you from creating those sub-Views in XAML? I would definitely define all my View code separately from the ViewModels. (This is the point of even having a ViewModel.)
If you define those Views in the tab control's XAML, you could bind to their DataContext the objects that you want to be their ViewModels, from the ViewModel of the tab control's View.
You can read my general thoughts on MVVM in this answer.
Upvotes: 2