Can I have multiple ViewModel for a View in WPF - mvvm-foundation

Can I have multiple ViewModel for a View in WPF? Because some times we need to show the view only in simple view format and sometimes the same view has to shown in editable format. Hence we can create seperate view models for each.
So is there any way to hook 2 diffrent view models to a view?

A ViewModel should serve as the Data Context for your View, so that would only allow a single VM.
There is no reason why you could not break you View up in to multiple UserControls, each with their own ViewModel.

Generally you're going to bind your View to a single type of View Model. That's not to say that your type couldn't be an interface, or a base class from which both of your views inherit.
I tend to only use an interface or base/derived class situation when I have a single view model type that is going to span several different views. For example if I have two different types of forms that display widget information, I would create a single base Widget ViewModel class.
In your case, it sounds you should either a.) create two views for your simple and advanced views or b.) simply use a single ViewModel class.

Related

Use the same user control xaml with different view models in Prism

I have an abstract base view model (IBaseViewModel) and two classes that implement it (Base1ViewModel, Base2ViewModel). I also have a Xaml user control, and I want in one page to use Base1ViewModel as view model and in another page Base2ViewModel as view model.
Prism best practice is to bind to view model by name, so what I ended up doing is duplicating the xaml file into Base1View and Base2View so each view gets the right view model, but this is obviously not ideal because then changes to the xaml should be duplicated as well.
Is there a solution/design in which I can reuse the same xaml user control with two different view models?
Thanks,
Noam

Constructing a repetitive UI in objective-c

In the past I have mainly created command-line programs, however I have recently started to venture into the world of GUI's and objective-c. I am trying to design an UI that has a repetitive structure of the same objects containing different data, and am uncertain as to the best way to create such a repetitive user interface.
For example, if you look at the Month View of Calendar.app, the interface is very repetitive. It is essentially a 7x6 grid of the same view (i.e. each day). My question is what would be the best way to go about creating such a repetitive view?
I initially saw two options:
Create the entire grid in the interface builder explicitly. But I see this as being rather cumbersome and presenting a load of hassle in controlling the individual objects later in code.
Creating a custom view for the grid, which is then programmatically populated by a series of 'day' custom views. I.e. create 42 instances of the same 'day' View within the custom view to form the grid.
In the case of the second I thought an array of DayView View Controllers may provide the solution but wasn't able to get very far with this solution.
Is one of these options the best way to go about achieving this task, or is there a better way? And how would one go about creating this?
UITableView and UICollectionView are great views to display this "repetitive" ui you call. These repetitive views are cells that are presented into sections (in a table view). I suggest you search for documentation about these classes.
Table view programming guide:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewStyles/TableViewCharacteristics.html#//apple_ref/doc/uid/TP40007451
UICollectionView class reference:
https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionView_class/Reference/Reference.html

Two views of one controller presenting the same data in different ways - possible using storyboard?

I have a controller that will send a set of data to the view. This set of data must be viewable in two different ways (no, this is not about landscape/portrait), thus two different views. My question is, how do I create these two views, linked to one controller, using storyboard? I want to be able to see and edit both views without doing any ugly tricks.
In my experience it gets a bit messy when trying to deal with different "main" views in the context of one controller no matter what you do.
Basically you need to create another view right on top of your UIViewController's view in storyboard and make it hidden, connect its outlets to the controller and when a button that flips your presentation styles gets hit you need to either show or hide your second representation view like this:
- (void)btnAction:(id)sender {
self.secondView.hidden = !self.secondView.hidden;
}

Partial view rendering issue

in my project i have one view which renders from viewmodel
view model contains the properties as follows
Model1
2.List of Model1
3.List of Model2
and my view has two partialviews , from controller i have pass the Viewmodel to mainview and in mainview i pass model1 to first partial view and List to second partialview
now in first partial view i want to render a dropdown with the values binded to List
How can i do this?
is there any explicit way to do this.
i cant change my model..........
If you really need to make the list go into partialView1 and cant change the model signature (which is what you should do), you can use #Html.Action("ActionName", "ControllerName") to go to an action that returns a partial view. This would likely incur an extra database trip.
To do it the correct way, you should change the partial view signature to accept the entire viewmodel, or at least a model that encompasses both List and model1.

How do you make a view corresponding to a model subclass that holds view elements corresponding to the parent model class?

I have some UI elements that will share common parts with other UI elements. They correspond to model objects in an inheritance hierarchy.
As an example let's say all Layers can be toggled on/off. All WaveLayers are Layers which additionally define a .wav filename. And there is a specific concrete subclass of WaveLayer called GroovyLayer which requires additional parameters.
Visually I'd like to have a long panel where the top section holds the general Layer UI, the next section holds the WaveLayer-specific UI, and the last section holds the GroovyLayer-specific UI. There may be like 10 or so concrete subclasses, each which share the same top sections corresponding to Layer and WaveLayer.
How should I set this up in InterfaceBuilder? Do I start at the top (with a placeholder for the subclass stuff) or at the bottom (with placeholders for the parent classes' stuff)? How should I go about setting up these placeholders in InterfaceBuilder and substituting in the right NSViews at the right times?
I've got this sort of thing to work by using a blank "custom view" in IB and tying it to an outlet in my controller class, and then substituting that view with another view tied to my controller from elsewhere in another NIB, but that seems unelegant, as my controller then carries both "old view" and "new view" members...
Lay out the views however you wish. Then select the custom view that represents your subclass and set its class name in the inspector.
As to whether to load the views from separate xib files, I think that's overkill here. From your description these views are always visible together, so you gain nothing but complexity loading them from individual files.
The trick you seemed to be missing was setting the class name of the custom view to that of your subclass.