title: Accessing Properties in other ViewModels in MVVM Light
body: <p>I have a main <code>ViewModel</code> containing a List of items which I'm using in a certain amount of <code>UserControls</code>, which are displayed in a <code>ContentControl</code> on the main view. My current way of exchanging data between the <code>ViewModels</code> exists of making a reference to each of the <code>ViewModels</code> in the main <code>ViewModel</code>, and one of the main <code>ViewModel</code> in every <code>UserControl</code>. However I do not know is this is the right way of doing this, since I have a <code>ViewModelLocator</code> and I kind of expect this class to have some functionality to support something like this.</p>
  
  <p>Can anyone please tell me if what I'm doing this OK, or if there is a better way of doing this in MVVM Light?</p>
  
  <p>EDIT:</p>
  
  <p>I found this when I was searching for something different, would this be a better solution?</p>
  
  <pre><code>private ViewModelLocator locator = new ViewModelLocator();
  </code></pre>
  
  <p>And then using the locator Properties to reference each ViewModel?</p>
  
  <p>EDIT2:</p>
  
  <p>Apparently what I thought would work doesn't, at first I had only references in the main <code>ViewModel</code> and this worked, but when I try this in the <code>UserControls</code> it kind of crashes my app. I'm currently trying the possible solution of first edit.</p>
  
  <p>MainViewModel.cs (others are similar, with reference only to the main ViewModel)</p>
  
  <pre><code>public class MainViewModel : ViewModelBase
  {
      private ItemAddViewModel itemAddViewModel;
      private ItemsViewModel itemsViewModel;
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the MainViewModel class.
      /// &lt;/summary&gt;
      public MainViewModel()
      {
          ItemsList = Item.GetItemsList();
  
          itemAddViewModel = ServiceLocator.Current.GetInstance&lt;ItemAddViewModel&gt;();
          itemsViewModel = ServiceLocator.Current.GetInstance&lt;ItemsViewModel&gt;();
  
          ShowItemsView();
      }
  ...
      private void ShowItemsView()
      {
          CurrentControl = itemsViewModel;
      }
  ...
  </code></pre>