title: MVVM ViewModel inside ViewModel
body: <p>I'm trying to convert an existing Windows Form software to MVVM/WPF. I'm new to MVVM so I can make a lot of mistakes.</p>
  
  <p>In that app, these are my main objects simplified:</p>
  
  <p>The <strong>Product</strong> class, this class keep the info of every distinct product by the UPC. Also, there can be many products of the same UPC, but every physical item has a unique identifier, like UPC-date-serial, so this unique identifier is saves in a list inside the Product class. </p>
  
  <pre><code>class Product
  {
      // This is the barcode of the product
      public UPC
      { get; set; }
  
      // This is a list which contains the unique ID of every product 
      public List&lt;string&gt; ProductSerialCodes
      { get; set; }
  
  }
  </code></pre>
  
  <p>In that way, if we have 10 products of the same UPC, we have a single instance of the Product class with 10 items in the ProductSerialCodes list.</p>
  
  <p>Then, the <strong>Container</strong> class, this class is to identify every physical box filled with products. A box can contain different products.</p>
  
  <pre><code>class Container
  {
      // The box Id
      public string ContainerID
      { get; set; }
  
      // The list of products in the box
      public List&lt;Product&gt; ProductList
      { get; set; } 
  }
  </code></pre>
  
  <p>These classes have more code, and self validated functions, so I think is better to make them ViewModels.</p>
  
  <p>The ProductViewModel:</p>
  
  <pre><code>class ProductViewModel
  {
      List&lt;string&gt; _productSerialCodes = new List&lt;string&gt;();
  
      public string UPC
      { get; set; }
  
      public List&lt;string&gt; ProductSerialCodes
      { get { return _productSerialCodes; } }
  
      public int ItemsCount
      { get { return _productSerialCodes.Count; } }
  
      public AddItem(string itemID)
      {
          _productSerialCodes.Add(itemID);
          OnPropertyChanged("ItemsCount");
      }
  }
  </code></pre>
  
  <p>*Note: The AddItem() method is called from a background thread.</p>
  
  <p>The ContainerViewModel:</p>
  
  <pre><code>public class ContainerViewModel
  {
      private ObservableCollection&lt;ProductViewModel&gt; _productList;
  
      public ObservableCollection&lt;ProductViewModel&gt; ProductList
      { get { return _productList; } }
  
      public string ContainerID
      { get; set; }
  
      public AddItem(string itemID)
      {
          // This comes from a background thread.
          // I find the product related to the item,         
          // (If the product doesn't exist in the list I create it)
          _productList.Find(product).AddItem(itemId);
      }
  }
  </code></pre>
  
  <p>So this is my approach to MVVM, my MainViewModel has a ContainerViewModel and on the screen I want to display the list of products, and quantities, example:</p>
  
  <pre><code>UPC          Qty
  000000000000 12
  </code></pre>
  
  <p>As the data comes from a background thread, it must be updating by the INotifyPropertyChanged automatically.</p>
  
  <p>Another thing I've noticed is, I need to click somewhere in the window for the UI to update. After reading a few blogs, I've found that using a SynchronizationContext object can solve that issue:</p>
  
  <p>Then when OnPropertyChanged(propertyName) is called, I call this</p>
  
  <pre><code>SynchronizationContext _syncContext;
  _syncContext.Post(delegate { CommandManager.InvalidateRequerySuggested(); }, null);
  </code></pre>
  
  <p>but this only works when I'm making a test on the UI thread. In my new application, as the data comes from a background thread, SynchronizationContext  is null, so, this solution didn't work.</p>
  
  <p>I've already read Josh Smith's <a href="https://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="nofollow">article</a> and karl shifflett's <a href="https://karlshifflett.wordpress.com/2010/05/09/stuff-wpf-line-of-business-using-mvvm-video-tutorial/" rel="nofollow">Stuff</a>. These are great, btw.</p>
  
  <p>So, The questions are:</p>
  
  <p>-Is my approach correct? I'm open to new ideas.</p>
  
  <p>-How can I bind a ListBox, or other control to a property in a ViewModel inside another ViewModel?</p>
  
  <p>-How can I marshall the OnPropertyChanged to the UI thread, and the update is applied without the need to click on the window?</p>
  
  <p><strong>--EDIT</strong></p>
  
  <p>My main problem is displaying the data.</p>
  
  <p>If my MainViewModel looks like this:</p>
  
  <pre><code>public class MainViewModel
  {
  
      private ContainerViewModel _currentContainer = new ContainerViewModel();
  
      public ContainerViewModel CurrentContainer
      { get { return _currentContainer; } }
  
      // more code...
  }
  </code></pre>
  
  <p>Then in the View, to display the ProductList, my xaml is:</p>
  
  <pre><code>&lt;Window.DataContext&gt;
      &lt;ViewModels:MainViewModel /&gt;
  &lt;/Window.DataContext&gt;
  
  &lt;ListBox Grid.Column="0" Margin="5"
              ItemsSource="{Binding CurrentContainer.ProductList}"&gt;
      &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;DockPanel LastChildFill="True"&gt;
                  &lt;TextBlock Text="{Binding UPC}" /&gt;
                  &lt;TextBlock Text="{Binding ItemsCount}" /&gt;
              &lt;/DockPanel&gt;
          &lt;/DataTemplate&gt;
      &lt;/ListBox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <ul>
  <li><em>NOTE: The data template will be different, however, what I want is to see the items in the listbox.</em></li>
  </ul>
  
  <p>When I run the application, in background, the ViewModels are being populated. If I set a breakpoint, I can see items in the observable lists.</p>
  
  <p>But nothing on the screen, the ListBox remains empty</p>
  
  <p>Thanks for your time.</p>
  
  <p>Regards.</p>