title: WPF DataGrid - Databind to DataTable cell in CellTemplates DataTemplate
body: <p>I have a DataGrid with a DataTable with as ItemsSource. The number of columns differ from time to time. If the DataType of a column is of class A I want to use a DataTemplate to customize the appearance of the cell content.</p>
  
  <p>I have set </p>
  
  <pre><code>AutoGenerateColumns="True" 
  </code></pre>
  
  <p>on the DataGrid so that all columns in the DataTable will be generated.</p>
  
  <p>I replace the DataGridColumn with a DataGridTemplateColumn if the DataType is of type A</p>
  
  <pre><code>private void DataGrid_AutoGeneratingColumn(object sender, system.Windows.Controls.DataGridAutoGeneratingColumnEventArgs e)
  {
      if (e.PropertyType == typeof(A))
      {
          e.Column = new DataGridTemplateColumn
          {
              CellTemplate = (DataTemplate)Resources["ATemplate"],
              Header = e.Column.Header,
              HeaderTemplate = e.Column.HeaderTemplate,
              HeaderStringFormat = e.Column.HeaderStringFormat
          };
      }
  }
  </code></pre>
  
  <p>The DataTemplate looks like this.</p>
  
  <pre><code>&lt;DataTemplate x:Key="ATemplate"&gt;
     &lt;RadioButton Content="{Binding Name}" GroupName="{Binding GroupName}" IsChecked="{Binding IsSelected}" /&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>The radiobutton is shown, but I get binding errors for all properties, like</p>
  
  <pre><code>BindingExpression path error: 'IsSelected' property not found on 'object' ''DataRowView'
  </code></pre>
  
  <p>Class A looks like this</p>
  
  <pre><code>public class A
  {
      public string Name { get; set; }
      public string GroupName { get; set; }
      public bool IsSelected { get; set; }
  }
  </code></pre>
  
  <p>How can i databind the DataTemplate to the right cell and property?</p>
  
  <p>(If you have a MVVM solution in which I don't have to use DataGrid_AutoGeneratingColumn it would be great)</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have tried this solution too with no luck. Only the classname is shown in the cell as usual when it doesen't know how to render the class.</p>
  
  <pre><code>&lt;DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Items}"&gt;
     &lt;DataGrid.Resources&gt;
        &lt;DataTemplate DataType="{x:Type viewModel:A}"&gt;
           &lt;RadioButton Content="{Binding Path=Name}" GroupName="{Binding Path=GroupName}" IsChecked="{Binding Path=IsSelected}" /&gt;
        &lt;/DataTemplate&gt;
     &lt;/DataGrid.Resources&gt;
  &lt;/DataGrid&gt;
  </code></pre>