title: How to add child element to grid in WPF using MVVM pattern
body: <p>I am trying to add one child element to Grid control through binding in mvvm pattern instead of code behind.</p>
  
  <p>View Code:</p>
  
  <pre><code>&lt;Grid  Name="grid"&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>    public MainWindow()
      {
          InitializeComponent();
          AddControl();
      }
  
      private void AddControl()
      {
          Viewer viewer = new Viewer();
          grid.Children.Add(viewer.GetUIElement());
      }
  </code></pre>
  
  <p>The above code works fine but I want to do this in VM through binding instead of code behind.
  Please note that, in above code Viewer is the third party control and it add to the grid perfectly through code behind. I want to add Viewer control to the Grid dynamically using binding.</p>
  
  <p>It is not always necessary to have a Grid control there. It can be any control like a parent/hosting/content control. There is only one child control that is Viewer.</p>
  
  <p>I have a basic knowledge of binding like ICommand, INotifyPropertyChanged, Dependency Property etc., but now aware about how to add any control(Viewer in my case) into another parent control(Grid in my case).</p>
  
  <p>The code may be following: View and VM is bound through DataContext</p>
  
  <p>View Code:</p>
  
  <pre><code>&lt;ContentControl Content="{Binding MyControl}"&gt;&lt;/ContentControl&gt;
  </code></pre>
  
  <p>VM Code:</p>
  
  <pre><code>class VM
  {
      Viewer view;
      public VM()
      {
          view = new Viewer();
      }
      public Viewer MyControl
      {
          get {return view; }
          set{view= value;}
      }
  }
  </code></pre>