title: WPF binding to combobox in DataGridTemplateColumn
body: <p>I have a class in a WPF MVVM application which I would like to display in a popup form and manage some of its members.</p>
  
  <pre><code>public class Route
  {
      public string Name { get; set; }
      public List&lt;RouteSegment&gt; MandatorySegments { get; set; }
  }
  
  public class RouteSegment
  {
     public decimal Id { get; set; }
     public string Name { get; set; }
  }
  </code></pre>
  
  <p>I've only added a few of the member fields, the rest are not relevant.</p>
  
  <p>What I would like to do is to display all the items found in <code>MandatorySegments</code> list in a combobx in a datagrid so I can add and remove new members easily.</p>
  
  <p>This is the code I have so far:</p>
  
  <p>In my ViewModel</p>
  
  <pre><code>public RouteTest SelectedRoute { get; set; }
  public ObservableCollection&lt;RouteSegment&gt; AllRouteSegments { get; private set; }
  public RouteSegment SelectedMandatorySegment { get; set; }
  </code></pre>
  
  <p>In my View</p>
  
  <pre><code>&lt;DataGrid Grid.RowSpan="2"
                  AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="5,6,0,5" Name="dgMandatorySegments" VerticalAlignment="Stretch" Width="306"
                    ItemsSource="{Binding SelectedRouteTest.MandatorySegments, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" CanUserResizeRows="False" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserReorderColumns="False"
                        CanUserAddRows="{StaticResource False}" SelectionMode="Single" SelectedItem="{Binding SelectedMandatorySegment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"&gt;
                  &lt;DataGrid.Columns&gt;
                      &lt;DataGridTemplateColumn Header="Route Segment"&gt;
                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;ComboBox ItemsSource="{Binding DataContext.AllRouteSegments, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 
                                             ItemTemplate="{StaticResource CboxItemTemplate}"/&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                      &lt;/DataGridTemplateColumn&gt;
                  &lt;/DataGrid.Columns&gt;
              &lt;/DataGrid&gt;
  </code></pre>
  
  <p>What I'm missing is the <code>SelectedValue</code> or <code>SelectedItem</code> on the combobox in the templated grid column. Now, in the grid I have the same number of rows as elements in my list and the <code>ItemsSource</code> of the combobox binds properly, because I can choose from all the <code>RouteSegment</code> elements. But the initial value is always empty for all the rows in the datagrid.
  What should the <code>SelectedValue</code> of the combobox bind to?</p>