title: Assign a control template programmatically with XamDataGrid
body: <p>Good afternoon everyone,</p>
  
  <p>We have a XamDataGrid on our WPF application (VS 2010) and I need to programmatically add a control template for one field (fields are always added from code on our app).</p>
  
  <p>So instead of creating the template on the XAML I am creating it from code (I followed several posts that explain similar issues) like this:</p>
  
  <pre><code>        Field f = new Field { Name = name, Label = label, Width = new FieldLength(20) }; 
          Style cellStyle = new System.Windows.Style(); 
  
          string templateStr = "&lt;ControlTemplate TargetType=\"{x:Type igDP:CellValuePresenter}\"&gt;" + 
                                   "&lt;Button Content=\"Flu\" /&gt;" + 
                              "&lt;/ControlTemplate&gt;"; 
  
          ParserContext pc = new ParserContext(); 
          pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); 
          pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml"); 
          pc.XmlnsDictionary.Add("igDP", "http://infragistics.com/DataPresenter"); 
  
          ControlTemplate ct = (ControlTemplate)XamlReader.Parse(templateStr, pc); 
  
          cellStyle.Setters.Add(new Setter() { Property = CellValuePresenter.TemplateProperty, Value = ((object)new CellValuePresenter() { Template = ct }) }); 
          f.Settings.CellValuePresenterStyle = cellStyle; 
  
  
          MainDataGrid.FieldLayouts[0].Fields.Add(f); 
  </code></pre>
  
  <p>Somehow this is not working and none of my fields/columns is displayed. So this is not working, any help will be appreciated.</p>