title: How to bind a combobox inside wpf datagrid?
body: <p>How to bind (Itemssource and selected item) of a combobox inside wpf datagrid? I am using MVVM pattern.
  I have tried         </p>
  
  <pre><code>&lt;DataGridTemplateColumn Header="Example 9"&gt;
                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;ComboBox ItemsSource="{Binding PartIds, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding PartId, UpdateSourceTrigger=PropertyChanged}" /&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                      &lt;/DataGridTemplateColumn&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>   &lt;DataGrid AutoGenerateColumns="False" ItemsSource="{Binding MyCars}" HorizontalAlignment="Left"&gt;
                  &lt;DataGrid.Columns&gt;
                      &lt;DataGridTextColumn Header="Model" Binding="{Binding Model}"/&gt;
                      &lt;DataGridTextColumn Header="Registration" Binding="{Binding Registration}"/&gt;
  
                      &lt;DataGridTemplateColumn Header="Example 12"&gt;
                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;ComboBox ItemsSource="{Binding CarParts, RelativeSource={RelativeSource AncestorType=Window}}" DisplayMemberPath="PartName" SelectedValuePath="PartID"  SelectedValue="{Binding PartId, UpdateSourceTrigger=PropertyChanged}" /&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                      &lt;/DataGridTemplateColumn&gt;
  
                      &lt;DataGridTextColumn Header="Selected" Binding="{Binding PartId}"/&gt;
  
                  &lt;/DataGrid.Columns&gt;
              &lt;/DataGrid&gt;
  </code></pre>
  
  <p>Properties used for data-binding</p>
  
  <pre><code>#region DataGrid List&lt;String&gt; Example
  
          public ObservableCollection&lt;MyCar&gt; MyCars { get; set; }
          public List&lt;string&gt; PartIds { get; set; } 
  
          #endregion
  
          #region DataGrid List&lt;Class&gt; Example
  
          public List&lt;CarPart&gt; CarParts { get; set; }
  
          #endregion
  </code></pre>
  
  <p>Reference:
  <a href="http://code.msdn.microsoft.com/windowsdesktop/Best-ComboBox-Tutorial-5cc27f82" rel="nofollow">http://code.msdn.microsoft.com/windowsdesktop/Best-ComboBox-Tutorial-5cc27f82</a></p>