title: Silverlight Switching views in a view using MVVM Light framework
body: <p>I have searched and tried for days and finally must ask the question here.
  I have a Silverlight 5 application, Using MVVM Light, where I want to be able to dynamically switch views in the main view.</p>
  
  <p>For the sake of simplicity, lets say I have 2 buttons.</p>
  
  <p>Button1 will switch to TestView1.</p>
  
  <p>Button2 will switch to TestView2.</p>
  
  <pre><code> &lt;Button Content="TestView1" Grid.Column="1" Command="{Binding CallTestView1Command}" HorizontalAlignment="Left" Margin="185,17,0,0" VerticalAlignment="Top" Width="75"/&gt;
      &lt;Button Content="TestView2" Grid.Column="1" Command="{Binding CallTestView2Command}" HorizontalAlignment="Left" Margin="280,17,0,0" VerticalAlignment="Top" Width="75"/&gt;
  </code></pre>
  
  <p>The way I have done it is by binding a relaycommand to the button and then instanciating a new viewmodel of the corresponding view.
  ie:</p>
  
  <pre><code>private RelayCommand _callTestView1Command;
      public RelayCommand CallTestView1Command
      {
          get
          {
              return _callTestView1Command ??
                     (_callTestView1Command = new RelayCommand(() =&gt;
                         {
                             CurrentView = ViewModelLocator.NinjectKernel.Get&lt;TestViewModel1&gt;();
                         }));
          }
      }
  </code></pre>
  
  <p>The CurrentViewmodel is then set to the new viewmodel.
  In the MainView I have bound the CurrentView to a ContentControl:</p>
  
  <pre><code>&lt;Border x:Name="displayedView" Grid.Row="2"&gt;
          &lt;ContentControl Content="{Binding CurrentView}" /&gt;
      &lt;/Border&gt;
  </code></pre>
  
  <p>This will actually work to some extend, since the CurrentView will change but instead of actually showing the content of the view it simply shows the Namespace of the ViewModel that is instanciated.</p>
  
  <p>So far I have primarily used the knowledge taken from these sources:</p>
  
  <p><a href="http://rachel53461.wordpress.com/2011/05/28/switching-between-viewsusercontrols-using-mvvm/" rel="nofollow noreferrer">http://rachel53461.wordpress.com/2011/05/28/switching-between-viewsusercontrols-using-mvvm/</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5956559/loading-views-into-contentcontrol-and-changing-their-properties-by-clicking-butt">Loading Views into ContentControl and changing their properties by clicking buttons</a></p>
  
  <p>but they do not solve my problem, or I do not quite understand how to actually show the views.:-(</p>
  
  <p>So does anyone have a good explanation on how to switch the views correct in Silverlight 5 using MVVM Light from GalaSoft.</p>
  
  <p>Thanks</p>