title: Getting selected value of ComboBox in DataGrid
body: <p>How can I get the selected value of a combo box that is in a datagrid?</p>
  
  <p>I've simplefied my problem so that it is easier for someone to help. Here is the XAML:</p>
  
  <pre><code>&lt;Window x:Class="del_WpfApplication3.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;DataGrid DataContext="{Binding}" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="60,32,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="368"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Binding="{Binding test}"&gt;&lt;/DataGridTextColumn&gt;
                  &lt;DataGridTemplateColumn&gt;
                      &lt;DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;ComboBox ItemsSource="{Binding combo}" DisplayMemberPath="value" SelectedValuePath="key" SelectedItem="{Binding selected, Mode=TwoWay}"&gt;&lt;/ComboBox&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;/DataGridTemplateColumn&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
          &lt;Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="353,238,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Here is the code behind:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace del_WpfApplication3
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              load();
          }
  
          public class tmp1
          {
              public string test { get; set; }
              public IList&lt;tmp2&gt; combo { get; set; }
              public string selected { get; set; }
          }
  
          public class tmp2
          {
              public string key { get; set; }
              public string value { get; set; }
              public string selected { get; set; }
          }
  
          private void load()
          {
              IList&lt;tmp2&gt; list = new List&lt;tmp2&gt;();
              list.Add(new tmp2(){ key = "key1", value = "value1" });
              list.Add(new tmp2(){ key = "key2", value = "value2" });
  
              IList&lt;tmp1&gt; list2 = new List&lt;tmp1&gt;();
              list2.Add(new tmp1() { test = "test1", combo = list });
              list2.Add(new tmp1() { test = "test2", combo = list });
  
              dataGrid1.ItemsSource = list2;
  
  
          }
  
          private void button1_Click(object sender, RoutedEventArgs e)
          {
              tmp1 t = (tmp1)dataGrid1.Items[0];
              Console.WriteLine(t.selected); // this is empty when I'm expecting it to be the selected value as set in the XAML.
          }
      }
  }
  </code></pre>