title: How do I append items to a ListBox without violating MVVM
body: <p>I have set up a MVVM design pattern to the WPF application and I'm trying to think of the best way to append listbox items to a listbox and am assuming this should be done in the viewmodel. If this is the case, then i'm stuck. My mainwindow holds a contentcontrol that pulls in the appropriate view as a usercontrol. I'm assuming I would have to bind the itemssource of the listbox to a property in the viewmodel but I'm not sure how to attempt this as I don't have access to the listbox in the camera view. </p>
  
  <p>Should the dynamic data just pull in the items in the user controls constuctor (this seems wrong but would work).</p>
  
  <p>Any thoughts?</p>
  
  <p>No frameworks are used, custom MVVM pattern.</p>
  
  <p><strong>User Control</strong></p>
  
  <pre><code>&lt;ListBox x:Name="CameraList" Background="#ff4c4c4c" BorderThickness="0" 
   ScrollViewer.CanContentScroll="False" TouchEnter="CameraList_TouchEnter" 
   TouchLeave="CameraList_TouchLeave" 
   ManipulationBoundaryFeedback="CameraList_ManipulationBoundaryFeedback"
   ItemContainerStyle="{DynamicResource ResourceKey=ListBoxItemStyle}" 
   PreviewTouchDown="CameraList_PreviewTouchDown" 
   PreviewTouchMove="CameraList_PreviewTouchMove" 
   PreviewTouchUp="CameraList_PreviewTouchUp" 
   HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt;
      &lt;ListBox.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary Source="/ResourceLibrary;component/User 
                   Controls/Slider.xaml"&gt;
                  &lt;/ResourceDictionary&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/ListBox.Resources&gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>class CameraListViewModel : WorkspaceViewModel
  {
      #region Fields
      private readonly CameraRepository cameraRepository;
      #endregion
  
  
      #region Properties
      /// &lt;summary&gt;
      /// Gets and sets the cameras in the system.
      /// &lt;/summary&gt;
      public ObservableCollection&lt;CameraViewModel&gt; Cameras { get; private set; }
      #endregion
  
  
      #region Constructors
      public CameraListViewModel(CameraRepository cameraRepository)
      {
          if (cameraRepository == null)
          {
              throw new ArgumentNullException("cameraRepository");
          }
  
          base.DisplayName = "CameraList";
  
          this.cameraRepository = cameraRepository;
  
          // Populate the CameraList collection with CameraViewModel.
          this.CreateCameras();
      }
      #endregion
  
  
      #region Internal Members
      /// &lt;summary&gt;
      /// Create all the cameras in the system.
      /// &lt;/summary&gt;
      private void CreateCameras()
      {
          List&lt;CameraViewModel&gt; all =
              (from cam in cameraRepository.GetCameras()
               select new CameraViewModel(cam, cameraRepository)).ToList();
  
          foreach (CameraViewModel cvm in all)
          {
              cvm.PropertyChanged += this.OnCameraViewModelPropertyChanged;
          }
  
          this.Cameras = new ObservableCollection&lt;CameraViewModel&gt;(all);
          this.Cameras.CollectionChanged += this.OnCollectionChanged;
      }
      #endregion
  
  
      #region Events
      /// &lt;summary&gt;
      /// Handle changed collections.
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;&lt;/param&gt;
      /// &lt;param name="e"&gt;&lt;/param&gt;
      private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
          if (e.NewItems != null &amp;&amp; e.NewItems.Count != 0)
          {
              foreach (CameraViewModel cvm in e.NewItems)
              {
                  cvm.PropertyChanged += this.OnCameraViewModelPropertyChanged;
              }
          }
  
          if (e.OldItems != null &amp;&amp; e.OldItems.Count != 0)
          {
              foreach (CameraViewModel cvm in e.OldItems)
              {
                  cvm.PropertyChanged -= this.OnCameraViewModelPropertyChanged;
              }
          }
      }
  
      /// &lt;summary&gt;
      /// Handle property changes.
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;&lt;/param&gt;
      /// &lt;param name="e"&gt;&lt;/param&gt;
      private void OnCameraViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
      {
          string isSelected = "IsSelected";
  
          // Make sure that the property name we're referencing is valid.
          // This is a debugging technique, and does not execute in a Release build.
          (sender as CameraViewModel).VerifyPropertyName(isSelected);
  
          // When a camera is selected or unselected, we must let the system know
          // that properties have changed, so that it will be queried again for a new value.
          if (e.PropertyName == isSelected)
          {
              this.OnPropertyChanged("IsSelected");
          }
      }
      #endregion
  }
  </code></pre>
  
  <p>Currently this works and will display the listbox, however need a good way of merging what is in the <strong>camerarepository</strong> into the listbox. </p>