Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a View know what ViewModel to use in WPF?

Tags:

Can someone explain how the View and ViewModel are connected? I can't find anywhere the xaml or the xaml.cs for the View that references the ViewModel, nor anything in the ViewModel.cs file that references the View, yet they are somehow connected, and binding members from the ViewModel to the View work.

Also, in the constructor of each, there is only the InitializeComponent for the View and a basic constructor for the ViewModel (no declaration/definition of the View).

Thanks!

like image 889
Bob. Avatar asked Jun 21 '12 15:06

Bob.


People also ask

Does the view know about the ViewModel?

If you use Binding instead then you define a property of the ViewModel. So the View knows about the property of the ViewModel. It's just that Binding uses Reflection (not type safe) but the coupling is the same.

How do I link views and ViewModel?

The other approach to bind the View and Viewmodel in View First Approach is in the xaml itself as shown in the figure below. I am setting the datacontext in the . xaml of the View itself. To set the datacontext in this way the ViewModel class need to have a default constructor.

Can a view have multiple ViewModels WPF?

No that is fine; each object should be a ViewModel in its own right.

Should every view have a ViewModel?

For a best mvvm pattern implementation each view must have the own viewmodel, and don't share anythings with other.


2 Answers

There are various options here.

Something has to set the View's DataContext to be an instance of the ViewModel. There are lots of options here:

  • This can be done directly in xaml (the View just instances the ViewModel directly).
  • This can be done in the View's constructor (this.DataContext = new MyViewModel();)
  • This can be handled via a DataTemplate
  • A "coordinating" class can wire these together (ie: a separate "presenter" class can construct both and set the DataContext appropriately)

The most common are to either have the View define the VM in the xaml (View-first), or to have everything based from a ViewModel-centric point of view, and have WPF automatically create the View based on the bound VM (ViewModel-first).

The former approach is what's used by a lot of toolkits, such as MVVM Light. The latter approach is what I used in my MVVM blog series, and used by some other toolkits.

like image 84
Reed Copsey Avatar answered Sep 23 '22 04:09

Reed Copsey


A "clean" way for connecting the views to the view-models would be...

When you create the views, for each view, set its DataSource to its view-model:

E.g.

public class App {     private void OnAppStart()     {         var model = new MainModel();         var vm = new MainVM();         var view = new MainWindow();          vm.Model = model;         view.DataSource = vm;          view.Show();     } } 

When the model you are viewing changes, update the VM:

public class MainVM {     private void OnSelectedModelItemChanged()     {         this.SelectedItem = new ItemVM();         this.SelectedItem.Model = this.SelectedModelItem;     } } 

And use data templates to make view select the correct sub views for each VM.

like image 41
Danny Varod Avatar answered Sep 26 '22 04:09

Danny Varod