How to Inject multiple viewmodels in wpf by using mvvmtoolkit? - c#

As far as i know, One of the advantages of MVVM models is that the view model is an abstracted view, so multiple views can bind and use one view model.
So i tried to inject multiple ViewModels into single view by following code.
MainWindow.xaml.cs
this.DataContext = App.Current.Services.GetService<MainWindowViewModel>();
this.DataContext = App.Current.Services.GetService<AnotherViewModel>();
But i figured out that, theres App.Current.Services.GetServices
But i don't know how to utlize it.
Is it correct way to inject multiple viewmodels by above code?
How to Utlize App.Current.Services.GetServices?

Related

Dynamically creating ViewModel for View with many UserControls

I am working on a project in C# with many complex UserControls. Things like tanks, valves, pumps, gauges, etc. A view can have anywhere from a couple to 40 or 50 controls on it. I will ultimately have hundreds of views made up of these Controls. (I am generating them programmatically from old versions of the screens). The problem I have run into is creating a ViewModel for my views. I can not make a static ViewModel for every page. I need to dynamically create a ViewModel based on the controls contained in the view.
I am able to find what controls are on a view and create a model for each but I dont know how to create a ViewModel which lets me bind to those models without creating a ViewModel for each individual control.
Any help or advice would be appreciated, Thanks!
I think I was able to solve the problem. My ViewModel has a Dictionary with a unique Identifier as the key and the Model object as the value.
I go through the controls on a view, create a model and add it to the VM dictionary. My Binding is to the dictionary as such:
Vm has a dictionary called Models:
Dictionary<string, Model> Models;
View
{Binding Models[KEY].Property}

How can I reuse the same view with different ViewModel in wpf prism?

I have a View for Insert an entity.
I want to edit the same entity and I want to reuse the same view for edit.
Now, How can I reuse the same view with different ViewModel for edit?
I assume you're using the ViewModelLocator, otherwise you wouldn't even have to ask the question.
So drop the view model locator here, navigate to the view model you want (InsertViewModel or EditViewModel) and assign the same view to both view models via data template.
Why do you need two different view models?
Just merge the properties in one viewmodel.
Or better still, break the view into different UserControls for each view model.

MVVM: Pass Parameter from viewmodel to view

Im new to MVVM and XAML. Currently the application that I have to extend is some kind of imageviewer. It uses MVVM with a RelayCommand. Currently the code behind file of my XAML-file is almost empty.
Data is displayed with databinding to the viewmodel and user interaction is bound with command to the model.
But now I need to display an image that is an custom control. In XAML I have to place a Border-Control and than the control must be initialized in the code behind file.
My question is how to do the setup when following MVVM.
PseudoCode:
View{
initPDFControl(){
borderControl.DoMagic();
};
openImage(String path);
}
ViewModel{
openImage(){
getMyView.openImage(pathFromModel);
}
}
You can help me by linking me to helpful tutorials or note down some pseudocode.
From the example you have given it looks like you are not aware of the MVVM pattern. The view (XAML) is linked to the viewmodel class. The viewModel should be unaware of any Views. But the view is aware of its viewModel. You should write the commands in viewModel and not in the model. You have to access model class from the viewModel. First understand the pattern and then go forward to coding. There are many MVVM Libraries (for eg. MVVMLight, Prism) available in Nuget, you can use them in your code.
You can look at these links for understanding the concept.
Model-View-ViewModel-MVVM-Explained
WPF-MVVM-step-by-step-Basics-to-Advance-Level
MSDN-Implementing the MVVM Pattern Using the Prism Library

A ViewModel shared across multiples Views

I need some help about MVVM pattern.
I created a ViewModel that expose data and commands to be displayed in a listview in a View named A.
This ViewModel is also used in a view named B. In this view, i just need to expose some properties and no commands but i had to create 2 more properties.
Is it better to create a more specific ViewModel for View B even if it concerns the same object?
I would suggest composition, have two view models which both have a property containing a view model that holds the common properties. The two view models should then only have their specific other properties and commands.
Difficult to answer. But i can tell you what we do for our application. We have one viewmodel, which is more or less view independant, it just functions as a wrapper for our business data and contains all the stuff that is used in almost all parts where this model is shown. Now for the view part, we have very specific viewmodels. Like a ProjectTreeViewModel or a SearchResultViewmodel, with the corresponding Item viewmodels for both of them. The Item viewmodel doesn't need to implement all the logic again, it just needs to agregate the general model view model.
To give a better analogy:
If you have a File, Drive and Folder model. You would create a FileViewModel, DriveViewModel and FolderViewModel. But only one ExplorerItemViewModel. This only needs to provide a property to expose the underlying view model. The rest is depending on your data templates.

WPF MVVM Doubts

Hello fellow StackOverflow users (or Stackoverflowers?):
I'm learning-by-coding WPF. I read several articles/saw several screencasts, and coming from a WEB dev background, I fired up VS2010 and started doing a sample application that would help me learn the basics.
I read some about MVVM too, and started using it. I set up my solution to use WPF 4.0, ActiveRecord 2.1 and SQLite, and everything went kind well. But I still have some doubts:
I created a MainWindowViewModel, and am using the RelayCommand class from here to... relay the command. Am I breaking any guidelines by having a MenuItem from the MainWindow to have its command bound to a property of this viewmodel?
This action I'm binding the MenuItem command to is going to instantiate a new ViewModel and a new View, and show it. Again, is that ok in the MVVM context?
My MainWindow will be a kind of "dashboard", and I will have more than one model attached to this dashboard. Should I just wrap all those models in a single view model?
Something like this:
public class MainWindowViewModel {
private ObservableCollection<Order> openOrders;
private Address deliveryAddress;
private Order newOrder;
/* Wrappers for the OpenOrders Collection */
/* Wrappers for Delivery Address */
/* Wrappers for New Order */
/* Command Bindings */
}
TIA!
I created a MainWindowViewModel, and am using the RelayCommand class from here to... relay the command. Am I breaking any guidelines by having a MenuItem from the MainWindow to have its command bound to a property of this viewmodel?
No, you're not breaking any guideline. It's perfectly appropriate to bind the MenuItem to a command of the MainWindowViewModel (where else would you put this command anyway ?)
This action I'm binding the MenuItem command to is going to instantiate a new ViewModel and a new View, and show it. Again, is that ok in the MVVM context?
It's perfectly fine to create a new ViewModel, of course. As for creating a new view, it depends on how you create it... you should of course never instantiate a view explicitly from the ViewModel, because it would introduce a dependency of the VM to the view.
My MainWindow will be a kind of "dashboard", and I will have more than one model attached to this dashboard. Should I just wrap all those models in a single view model?
It depends on what you mean by "wrap"... Your MainWindowViewModel could expose other ViewModels through properties, and theses VMs would be displayed in different parts of the view. If that's what you mean, yes, you should wrap them.
Adding to the Thomas answer:
I would create different usercontrols for each part of the dashboard and assign a viewModel to each usercontrol.
I created a MainWindowViewModel, and am using the RelayCommand class from here to... relay the command. Am I breaking any guidelines by having a MenuItem from the MainWindow to have its command bound to a property of this viewmodel?
No, that's exactly where you put commands.
This action I'm binding the MenuItem command to is going to instantiate a new ViewModel and a new View, and show it. Again, is that ok in the MVVM context?
It shouldn't need to know how to instantiate a new view; that's the view's job. The specifics of how to do this depend on how you're showing this new view - it could be as simple as having a ContentPresenter in the view that's bound to a property in the view model, so when you set the property (and raise PropertyChanged) the ContentPresenter renders the new object with its related DataTemplate.
Things get a little hinky if by "instantiate a new view" you mean "open a new window." There's not an especially elegant way to do this, especially if you want the new window to be a modal dialog. One way is to add an event handler to the view's code-behind that listens to PropertyChanged on the view model; when the subordinate view model property gets set, the code in the view creates and shows the new window.
My MainWindow will be a kind of "dashboard", and I will have more than one model attached to this dashboard. Should I just wrap all those models in a single view model?
Sure. That's a really common pattern. It's not at all uncommon, for instance, to expose an observable collection property and bind an ItemsControl of some kind to it; the view will automagically create views for every view model you put in that collection. Again, the specific implementation really depends on your application.

Categories

Resources