title: C# WPF - DataGridComboBoxColumn ItemsSource
body: <p>I'm currently struggling with DataGridComboBoxColumn in C# WPF. </p>
  
  <p>I have the class ToolModel </p>
  
  <pre><code>class ToolModel
  {
      public long Id { get; set; }
      public string Number { get; set; }
      public string Name { get; set; }
      public string Coating { get; set; }
      public bool Thread { get; set; }
      public string Kind { get; set; }
      public ToolTypeModel Type { get; set; }
  }
  </code></pre>
  
  <p>and the class ToolTypeModel</p>
  
  <pre><code>public class ToolTypeModel
  {
      public long Id { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>The data is stored into a database and loaded into my ViewModel</p>
  
  <pre><code>class ToolsViewModel
  {
      public ObservableCollection&lt;ToolModel&gt; Tools { get; set; }
      public ObservableCollection&lt;ToolTypeModel&gt; ToolTypes { get; set; }
  
      public ToolsViewModel()
      {
          Tools = new ObservableCollection&lt;ToolModel&gt;(ToolModel.GetTools());
          ToolTypes = new ObservableCollection&lt;ToolTypeModel&gt;(ToolTypeModel.GetToolTypes());
      }
  }
  </code></pre>
  
  <p>I want to display the data in a DataGrid and attempted to add a DataGridComboBoxColumn to select a ToolType for the correspondending Tool. Therefore I added following xaml definition (wich is already a workaround - but the only way I was able to get it nearly working): </p>
  
  <pre><code> &lt;DataGrid x:Name="ToolsDataGrid" ItemsSource="{Binding Tools}" AutoGenerateColumns="False"&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Header="Nummer" Binding="{Binding Number}" /&gt;
              &lt;DataGridComboBoxColumn 
                      Header="Typ"
                      SelectedValueBinding="{Binding Type, Mode=TwoWay}" 
                      SelectedValuePath="Id"
                      DisplayMemberPath="Name"&gt;
  
                  &lt;DataGridComboBoxColumn.ElementStyle&gt;
                      &lt;Style TargetType="{x:Type ComboBox}"&gt;
                          &lt;Setter Property="ItemsSource" Value="{Binding Path=DataContext.ToolTypes, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" /&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridComboBoxColumn.ElementStyle&gt;
                  &lt;DataGridComboBoxColumn.EditingElementStyle&gt;
                      &lt;Style TargetType="{x:Type ComboBox}"&gt;
                          &lt;Setter Property="ItemsSource" Value="{Binding Path=DataContext.ToolTypes, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" /&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridComboBoxColumn.EditingElementStyle&gt;
              &lt;/DataGridComboBoxColumn&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  </code></pre>
  
  <p>It shows all elements of ToolTypes <strong>but</strong> not the selected one my Tool has a reference on. </p>
  
  <p>Any ideas how I can look up the ToolTypes into the ComboBoxColumn and show the referenced Type as selected item? </p>
  
  <p>Thanks in advance. </p>