tags:

views:

54

answers:

1

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?

+2  A: 

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.

Venemo
Thanks for the response. This does make sense. I've been spending a lot of time learning MVVM lately and am still trying to determine what is kosher or not in the View vs. ViewModel. Your reply helps a lot - thanks.
j0rd4n
@j0rd4n - You're welcome. :)I've been using MVVM (and now also MVC) a lot lately. The main point of MVVM is separating the UI from the application logic, so the ViewModels must not know anything about the Views.
Venemo
MVVM is also not always the correct answer. There are other patterns like MVC and MVP that are appropriate when necessary, it is about the right tool for the job. You could treat your entry screen as a Website and its many views as pages, this might induce you to implement an MVC + Routing pattern on the navigation page. There is nothing wrong with mixing patterns as long as it all makes sense, you also don't want to force a pattern like MVVM into a situation where it doesn't belong.
Agies
@Agies - I agree. However, MVVM plays with WPF very nicely most of the time.
Venemo
@Venemo I agree for me this is a matter of preference. MVVM's purpose is to intercept how the View interacts with the Model. In the above question, there really isn't a Model just a View and something that needs to control it. To me the question above is in need of a Supervising or Passive Controller, where the testability is coming out of the controls behavior and not business logic affecting the model. By using an IoC and interfaces there won't be any hard dependencies on the View objects.
Agies
@Agies - I agree. However, we don't know what models are there in this scenario.
Venemo