Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing models to viewmodels when using a viewmodellocator
    primarykey
    data
    text
    <p>I'm trying to get to grips with the concept of a ViewModelLocator (in MVVM Light, although the question applies to the concept of a ViewModelLocator in general, regardles of which MVVM framework is used), and I have a hard time figuring out how to use it.</p> <p>As I understand it, your views use one of the properties on the singleton instance of the locator as their datacontext. The locator defines these various properties, and returns the proper viewmodel instance for each.</p> <p>That's all fine, but I have trouble understanding how you actually populate these viewmodels with the model data the views are supposed to present.</p> <p>For instance, suppose I have a view that presents a list of employees. I can create an EmployeesView and an EmployeesViewModel. In the ViewModelLocator, I can create a property that returns this EmployeesViewModel:</p> <pre><code>public EmployeesViewModel Employees { get { return ServiceLocator.Current.GetInstance&lt;EmployeesViewModel&gt;(); } } </code></pre> <p>Now, the viewmodel needs a list of employees, so I can create some sort of dataservice that returns all employees, and register that with the Servicelocator in the ViewModelLocator's constructor:</p> <pre><code>public ViewModelLocator() { ServiceLocator.SetLocatorProvider(() =&gt; SimpleIoc.Default); SimpleIoc.Default.Register&lt;IDataService, AllEmployeesDataService&gt;(); } </code></pre> <p>So, this'll work, if I instantiate the EmployeesView the EmployeesViewModel will be instantiated and injected with a dataservice that returns all employees.</p> <p>However, now I want to see the details of a certain employee that I just clicked in the EmployeesView. This employee presumably has some sort of Id by which to retrieve him/her from a database, or whatever.</p> <p>I can create an EmployeeDetailsView and an EmployeeDetailsViewModel, and add a property to the ViewModelLocator:</p> <pre><code>public EmployeeDetailsViewModel EmployeeDetails { return ServiceLocator.Current.GetInstance&lt;EmployeeDetailsViewModel&gt;(); } </code></pre> <p>and perhaps register some sort of dataservice in the ViewModelLocator's constructor:</p> <pre><code>SimpleIoc.Default.Register&lt;IDataService, EmployeeDetailsDataService&gt;(); </code></pre> <p>But how do I tell either the dataservice or the viewmodel which employee they're supposed to present the details for? Where do I pass the employee id? </p> <p>Am I looking at this all wrong? Anyone know of any good examples? All the examples I can find just return the same single instance of each viewmodel.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload