title: Create a grid in WPF as Template programmatically
body: <p>I want to create a basic user control with a style programmatically.
  In this style I want to add a <code>Grid</code> (no problem), but I can't add column definitions to this grid.</p>
  
  <p>My example code is</p>
  
  <pre><code>ControlTemplate templ = new ControlTemplate();
  FrameworkElementFactory mainPanel = new FrameworkElementFactory(typeof(DockPanel));
  mainPanel.SetValue(DockPanel.LastChildFillProperty, true);
  
  FrameworkElementFactory headerPanel = new FrameworkElementFactory(typeof(StackPanel));
  headerPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
  headerPanel.SetValue(DockPanel.DockProperty, Dock.Top);
  mainPanel.AppendChild(headerPanel);
  
  FrameworkElementFactory headerImg = new FrameworkElementFactory(typeof(Image));
  headerImg.SetValue(Image.MarginProperty, new Thickness(5));
  headerImg.SetValue(Image.HeightProperty, 32d);
  headerImg.SetBinding(Image.SourceProperty, new Binding("ElementImage") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
  headerPanel.AppendChild(headerImg);
  
  FrameworkElementFactory headerTitle = new FrameworkElementFactory(typeof(TextBlock));
  headerTitle.SetValue(TextBlock.FontSizeProperty, 16d);
  headerTitle.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
  headerTitle.SetBinding(TextBlock.TextProperty, new Binding("Title") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
  headerPanel.AppendChild(headerTitle);
  
  FrameworkElementFactory mainGrid = new FrameworkElementFactory(typeof(Grid));
  FrameworkElementFactory c1 = new FrameworkElementFactory(typeof(ColumnDefinition));
  c1.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Star));
  FrameworkElementFactory c2 = new FrameworkElementFactory(typeof(ColumnDefinition));
  c2.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Auto));
  FrameworkElementFactory c3 = new FrameworkElementFactory(typeof(ColumnDefinition));
  c3.SetValue(ColumnDefinition.WidthProperty, new GridLength(3, GridUnitType.Star));
  FrameworkElementFactory colDefinitions = new FrameworkElementFactory(typeof(ColumnDefinitionCollection));
  colDefinitions.AppendChild(c1);
  colDefinitions.AppendChild(c2);
  colDefinitions.AppendChild(c3);
  mainGrid.AppendChild(colDefinitions);
  
  mainPanel.AppendChild(mainGrid);
  
  FrameworkElementFactory content = new FrameworkElementFactory(typeof(ContentPresenter));
  content.SetBinding(ContentPresenter.ContentProperty, new Binding() { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent), Path = new PropertyPath("Content") });
  mainGrid.AppendChild(content);
  
  templ.VisualTree = mainPanel;
  Style mainStyle = new Style();
  mainStyle.Setters.Add(new Setter(UserControl.TemplateProperty, templ));
  this.Style = mainStyle;
  </code></pre>
  
  <p>But the creation of <code>FrameworkElementFactory</code> with type <code>ColumnDefinitionCollection</code> will throw an exception <code>"'ColumnDefinitionCollection' type must derive from FrameworkElement, FrameworkContentElement, or Visual3D."</code></p>
  
  <p>Who can help me?</p>