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

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!

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

Related

MVVM - Menu View with Preview View (Shared ViewModel) approach

I've just started my journey with MVVM and WPF and I need an advice to solve my problem or rather guide me towards right solution.
Link to image: https://s12.postimg.org/md0h6fv3x/Simple_App.png
Description (what I want to achieve):
View A is a menu (UserControl) with ListBox and DataContext: V.ObservableCollection
View B is a preview to selected item from View A ListBox
View A & B are loaded from MainWindow
What is simpliest and cleanest approach to achieve that?
I was thinking about sharing the same ViewModel with View A&B.
Create a SelectedItem property in ViewModel and then put it MainWindow's resources and bind datacontext to View A & B
Is it valid approach? Will that VM be updated properly in both views if it get changed?
Somehow bind View's A listbox SelectedItem to View B preview mode but don't know how.
Use only one ViewModel.
Eg; Pseudo code :
public class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<Employee>{get;}
/* provide change notification in ChosenItem */
public Employee ChosenItem{get;set;}
}
Use this ViewModel as DataContext for both Views.

Correct MVVM design patterns - ViewModel and DataContext

I started playing around with Windows Phone development. I am using MVVM ligth and I am trying to follow good practices. But lately I ran into an issue:
Is it okay to use ViewModel inside a ViewModel as a property?
My scenario is that I am having a MainViewModel which is a Panorama object. This Panorama objects consists of few PanaoramaItems (Notes, Calender and About Controls/Pages).
My question is, if I have for the MainPage.xaml a MainViewModel, is it okay if I put the other ViewModels as properties (NotesViewModel, CalenderViewModel, AboutViewModel) to the MainViewModel, in that case those will inherit from the MainPage the DataContext and I would just bind to a property of the MainViewModel. Or should I rather use the locator pattern to allow the other pages/control to get their own ViewModels and do not inherit the DataContext?
Is it okay, if a control has a ViewModel or should it be rather for pages?
If the Parent and Child ViewModels are related: sure, that is fine, it does not violate the pattern.
This setup allows you to re-use ViewModels and Views across pages and controls.

DataTemplate for View made up of different Views

In a Windows 8 style app I have a View (or page) which is made up of several other Sub-Views.
Each of these has an associated ViewModel and they are defined and bounded via the MVVM Light ViewModelLocator
I then have a View2 which is made up of other Sub-Views which, again, are defined and bounded via the MVVM Light ViewModelLocator
What I want to do is to place instances of the View and View2 controls into a List on a MasterViewModel and then bind this list to a GridView on a MasterView file.
Each of the instances of View and View2 should render as they do if you were to create a single instance without placing it into a list.
What I have tried to do is create a List of Pages (as all views are instances of this type) and bind the GridView to this, but when I run the app the GridView appears empty.
I have seen an examples that use a DataTemplateSelector (http://www.wiredprairie.us/blog/index.php/archives/1705) and apply a DataTemplate to each of the items in the MasterView list.
I understand what this does, but what I don’t understand is how to create a DataTemplate for a View that is made up of other Views.
I’m sure I am overlooking something and / or just making this harder than it needs to be!
Thank you for any help :)
Normally in this case, you would be binding a list to a set of ViewModels, not views, and the data template selector (or ValueConverter depending on your flavour) would select and instantiate a view suited to the view model.
So if ViewA uses ViewModelA : ISubViewViewModel, and ViewB uses ViewModelB : ISubViewModel then your main ViewModel would contain a List<ISubViewModel> which has a set of ViewModelA & ViewModelB within it. The main view's ListBox is bound to the List<ISubViewModel> and the data template selector / ValueConverter resolves ViewA for ViewModelA's and ViewB for ViewModelB's.
Hope that makes sense, it's about as hard to describe the solution as it probably was to describe the problem. :)

Can MVVM Usercontrols have property defined in codebehind?

I have a WPF user control ...which is in MVVM. The user control(which contains a listview) need data from the page (where it is included). I have to set a property to get this data input. Will this comply with MVVM...if not, what is the way for the same?
I'm afraid this won't be correct in MVVM design pattern. try to stick to your view model to define properties. Why don't you consider moving that property to control's vm?
Use an ObservableCollection rather.
ObservableCollection<myModel> myOC = new ObservableCollection<myModel>();
where myModel is a class that has to be constructed transforming your columns in the DataTable to Properties.
In your MainViewModel, loop through the DataReader and create myOC out of it.
Now bind myOC to a ListView in your page.
The DataTemplate of ListView should be a view(UserControl) drawing data from a ViewModel constructed out of myModel
But your UserControl has the entire ListView inside. If that is on purpose, then let me know the entire design to give a better idea.

MVVM multiple views

Im trying to learn MVVM so far its going well , I have stumble on a situation which I don't know how to implement ..
What I want :
- A view with left navigation and right details pane .. right details will have a contentcontainer which would hold my User Controls for the views to be selected by left pane
What I have :
- A MainViewModel
- ViewModels for Each of the Entities I have on my database
My Problem :
- Since I will have an ObservableCollection of my VIEWMODELS in my MAINVIEWMODEL ( as per the example i'm patterning my application ) do I create a public property for each of my MODEL Entities which will be used for the databinding I have defined in my UserControls ??
You can create a Base Type for all of your ViewModels to be displayed on the right side.(lets call that as BaseContentViewModel)
Then your left side ListBox will be bind to ObservableCollection<BaseContentViewModel> and the SelectedValue of the ListBox will bind to a new MainViewModel.SelectedContent proeprty of Type BaseContentViewModel.
And on the right side you need to have a ContentControl to which SelectedContent bind to ContentControl.Content
Now it is just a matter of defining different UserControls as DataTemplates in the Resource XAMLs (Usually refers as ViewToViewModelMapping in MVVM)
Have a look at John Papa's PDC talk 'Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight'. In it he illustrates an approach for managing an application that has multiple MVVM triads.

Resources