title: How to add a WPF grid control from its XAML string at runtime?
body: <p>Let's say we have a grid XAML like below - eg. a generated string returned from a method.</p>
  
  <pre><code>&lt;Grid&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width='*' /&gt;
          &lt;ColumnDefinition Width='*' /&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;TextBlock Text='id' Grid.Column='0'/&gt;
      &lt;Rectangle Fill='Black' Grid.Column='1' /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>What I want to do is to create such a grid and added to a stackpanel at run time, codes similar as below.</p>
  
  <pre><code>XmlReader xr = XmlReader.Create(input: new StringReader(g.xaml));
  var control = XamlReader.Load(xr) as Grid;
  this.stackPanel.Children.Add(control);
  </code></pre>
  
  <p>The form I use is:</p>
  
  <pre><code>&lt;Window x:Class='AllRibbonBrushes.MainWindow'
          xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
          xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
          Title='MainWindow' Height='223' Width='533' 
          Loaded='Window_Loaded'&gt;
      &lt;ScrollViewer&gt;
         &lt;StackPanel Name="stackPanel"&gt;
            &lt;!--The runtime grid need to be added here--&gt;
         &lt;/StackPanel&gt;
      &lt;/ScrollViewer&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>But I get the error <code>Cannot create unknow type 'Grid'</code>. I succeed doing this by adding a button/a textblock but failed to add a grid with nested controls. </p>
  
  <p>If you know how to do so, please share. All helps are welcome and very much appreciated!</p>