title: How to dynamically add a converter in code-behind
body: <p>I am Binding a Datagrid to dynamic data via IDictionary: <a href="http://www.scottlogic.co.uk/blog/colin/2009/04/binding-a-silverlight-datagrid-to-dynamic-data-via-idictionary/comment-page-1/#comment-8681" rel="nofollow noreferrer">http://www.scottlogic.co.uk/blog/colin/2009/04/binding-a-silverlight-datagrid-to-dynamic-data-via-idictionary/comment-page-1/#comment-8681</a></p>
  
  <p>But I do not want to define any columns in the xaml (below is how it is done in Colin Eberhardt's post</p>
  
  <pre><code>&lt;data:DataGrid.Columns&gt;
      &lt;data:DataGridTextColumn Header="Forename" Binding="{Binding Converter={StaticResource RowIndexConverter}, ConverterParameter=Forename}" /&gt;
  &lt;/data:DataGrid.Columns&gt;
  </code></pre>
  
  <p>So I have written the following code to try and do the same thing in the code behind, but the code does not call the RowIndexConverter. Something must be missing.</p>
  
  <p>Code:</p>
  
  <pre><code>// add columns
  
  DataGridTextColumn textColumn = new DataGridTextColumn();
  textColumn.Header = "Forename";
  
  Binding bind = new Binding("Forename");
  bind.Converter = new RowIndexConverter() ;
  bind.ConverterParameter = "Forename";            
  
  textColumn.Binding = bind;
  _dataGrid.Columns.Add(textColumn);
  </code></pre>
  
  <p>Rest of the code (here for context):</p>
  
  <pre><code>// generate some dummy data
  Random rand = new Random();
  for (int i = 0; i &lt; 200; i++)
  {
      Row row = new Row();
      row["Forename"] = s_names[rand.Next(s_names.Length)];
      row["Surname"] = s_surnames[rand.Next(s_surnames.Length)];
      row["Age"] = rand.Next(40) + 10;
      row["Shoesize"] = rand.Next(10) + 5;
  
      _rows.Add(row);
  }
  
  // bind to our DataGrid
  _dataGrid.ItemsSource = _rows;    
  
  public class Row
  {
      private Dictionary&lt;string, object&gt; _data = new Dictionary&lt;string, object&gt;();
  
      public object this[string index]
      {
          get
          {
              return _data[index];
          }
          set
          {
              _data[index] = value;
          }
      }
  }
  </code></pre>