title: Building a DataTemplate in C#
body: <p>I am trying to build the following DataTemplate in C#</p>
  
  <pre><code>&lt;DataTemplate x:Key="lbl"&gt;
          &lt;!-- Grid 2x2 with black border --&gt;
          &lt;Border BorderBrush="Black"&gt;
              &lt;Grid&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition /&gt;
                      &lt;ColumnDefinition /&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition /&gt;
                      &lt;RowDefinition /&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;!-- x-coordinate --&gt;
                  &lt;TextBlock Text="X=" /&gt;
                  &lt;TextBlock Grid.Column="1" Text="{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}"/&gt;
                  &lt;!-- y-coordinate --&gt;
                  &lt;TextBlock Grid.Row="1" Text="Y=" /&gt;
                  &lt;TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Value, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}" /&gt;
              &lt;/Grid&gt;
          &lt;/Border&gt;
      &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>I have gotten this far and I can't figure out the <code>Grid.ColumnDefinitions</code> and I am getting an exception of</p>
  
  <blockquote>
    <p>FrameworkElementFactory must be in a sealed template for this operation.</p>
  </blockquote>
  
  <pre><code>private static DataTemplate GetToolTipsDataTemplate()
  {
          FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));
  
          FrameworkElementFactory x = new FrameworkElementFactory(typeof(TextBlock)); 
          x.SetBinding(TextBlock.TextProperty, new Binding("X="));
          grid.AppendChild(x);
  
          FrameworkElementFactory xValue = new FrameworkElementFactory(typeof(TextBlock));
          xValue.SetValue(TextBlock.TextProperty, "{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}");
          grid.AppendChild(xValue);
  
          FrameworkElementFactory y = new FrameworkElementFactory(typeof(TextBlock));
          y.SetBinding(TextBlock.TextProperty, new Binding("Y="));
          grid.AppendChild(y);
  
          FrameworkElementFactory yValue = new FrameworkElementFactory(typeof(TextBlock));
          yValue.SetValue(TextBlock.TextProperty, "{Binding Path=Values, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}");
          grid.AppendChild(yValue);
  
          FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
          border.SetValue(Border.BorderBrushProperty, System.Windows.Media.Brushes.Black);
          border.AppendChild(grid);  
  
          DataTemplate dt = new DataTemplate {VisualTree = border};
          return dt;
      }
  </code></pre>
  
  <p>Any help would be appreciated.</p>