title: WPF/MVVM Load an UserControl at Runtime
body: <p>i know that there a many articles about my problem but i cant find a solution.
  I am new in WPF - MVVM and i try to understand the MVVM-Logic.
  So i made a little project to understand that.
  For my later apps i want to load UserControls dynamicly to my Window.</p>
  
  <p>In my StartView i have a Binding to the StartViewModel.
  (The Binding is in the APP.xaml)</p>
  
  <pre><code>StartView app = new StartView();
  StartViewModel context = new StartViewModel();
  </code></pre>
  
  <p>The StartView</p>
  
  <pre><code>&lt;Window x:Class="test.Views.StartView"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:views="clr-namespace:test.ViewModel"
          Title="Window1" Height="300" Width="516"&gt;
      &lt;Grid&gt;
          &lt;Menu IsMainMenu="True" Margin="0,0,404,239"&gt;
              &lt;MenuItem Header="_Einstellungen"&gt;
                  &lt;MenuItem Header="Server" /&gt;
              &lt;/MenuItem&gt;
          &lt;/Menu&gt;
          &lt;ContentControl Content="{Binding LoadedControl}" Margin="0,28,0,128" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>the StartViewModel</p>
  
  <pre><code>namespace test.ViewModel
  {
      public class StartViewModel : ViewModelBase
      {
          #region Fields
          private UCStastistikViewModel _loadedControl;
          #endregion
  
          public StartViewModel()
          {
              LoadedControl = new UCStastistikViewModel();
          }
  
          #region Properties / Commands
          public UCStastistikViewModel LoadedControl
          {
              get { return _loadedControl; }
              set
              {
                  if (value == _loadedControl)
                      return;
  
                  _loadedControl = value;
                  OnPropertyChanged("LoadedControl");
              }
          }
          #endregion
  
          #region Methods
  
          #endregion
      }
  }
  </code></pre>
  
  <p>UCStatistikView</p>
  
  <pre><code>&lt;UserControl x:Class="test.Views.UCStatistik"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:vm="clr-namespace:test.ViewModel"
               mc:Ignorable="d" 
               d:DesignHeight="188" d:DesignWidth="508"&gt;
      &lt;UserControl.DataContext&gt;
          &lt;vm:UCStastistikViewModel /&gt;
      &lt;/UserControl.DataContext&gt;
      &lt;Grid Background="red"&gt;       
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>UCStatistikViewModel</p>
  
  <pre><code>namespace test.ViewModel
  {
      public class UCStastistikViewModel : ViewModelBase
      {
          #region Fields
          #endregion
  
          public UCStastistikViewModel()
          {
          }
  
          #region Properties / Commands
          #endregion
  
          #region Methods
          #endregion
      }
  }
  </code></pre>
  
  <p>Now i want to load my UCStatistikView in the ContentControl of my StartView.
  But in the Startview only the Path test.UCStatistikViewModel is shown instead of the whole UC
  Can anybody give me some Ideas where my problem is / where im am going wrong ?</p>
  
  <p>Bye j</p>