title: Binding WPF Combobox to DataTable via XAML
body: <p>I have a DataGrid that I am attempting to populate using a dynamic DataTemplate (depending on context, it will show a TextBlock, CheckBox, ComboBox, etc); I have the DataGrid bound to an object containing the data I need, but I'm running into trouble when it comes time to set the ItemsSource of the ComboBox cells to a DataTable contained in my object:</p>
  
  <pre><code> public class Attribute
  {
      public int ID { get; set; }
      public string Name { get; set; }
      public string Type { get; set; }
      public string Value { get; set; }
      public SqlDataSet.OptionDataTable Options { get; set; }
  }
  ...
  List&lt;Attribute&gt; attributes = new List&lt;Attribute&gt;();
  ...
  datagrid.ItemsSource = attributes;
  </code></pre>
  
  <p>Unless I misunderstand the method I'm using for the dynamic cell types (which is quite likely, as this is my first WPF project), I don't believe I have the option of binding my ComboBoxes directly to my Attribute.Options via back-end code, which seems to be the common answer to this situation.</p>
  
  <p>My XAML:</p>
  
  <pre><code>&lt;DataGrid.Resources&gt;
      &lt;DataTemplate x:Key="StringTemplate"&gt;
          &lt;TextBlock Text="{Binding Value}"/&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate x:Key="OptionTemplate"&gt;
          &lt;ComboBox ItemsSource="{Binding Source={StaticResource Options}}"
                    DisplayMemberPath="Description"
                    SelectedValuePath="OptionID"/&gt;
      &lt;/DataTemplate&gt;
      &lt;local:DataGridCellTemplateSelector x:Key="TemplateSelector" 
                                          StringTemplate="{StaticResource StringTemplate}" 
                                          OptionTemplate="{StaticResource OptionTemplate}"/&gt;
  &lt;/DataGrid.Resources&gt;
  &lt;DataGrid.Columns&gt;
      &lt;DataGridTemplateColumn Header="Value"
          CellTemplateSelector="{StaticResource TemplateSelector}"/&gt;
  &lt;/DataGrid.Columns&gt;
  </code></pre>
  
  <p>The ComboBoxes appear correctly when run in this fashion, but the contents are blank.</p>
  
  <p>And my DataGridCellTemplateSelector (<a href="http://social.msdn.microsoft.com/Forums/vstudio/en-US/5ffa3a6a-cea4-411e-aba5-0d7807b3949b/change-cell-datatype-in-a-grid-control-wpf-mvvm?forum=wpf" rel="nofollow">based heavily on this</a>):</p>
  
  <pre><code>public class DataGridCellTemplateSelector : DataTemplateSelector
  {
      public DataTemplate StringTemplate { get; set; }
      public DataTemplate OptionTemplate { get; set; }
  
      public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
      {
          if (item is Attribute)
          {
              switch ((item as Attribute).Type)
              {
                  case "Text":
                      return StringTemplate;
                  case "Option":
                      return OptionTemplate;
                  default:
                      return null;
              }
          }
          return null;
      }
  }
  </code></pre>
  
  <p>If there's a way to access the ComboBox to set the ItemsSource via code, that would be awesome as well. Thanks in advance for your time.</p>