title: Bind selected value of a multicolumn combobox inside DataGrid
body: <p><strong>Working Example :</strong></p>
  
  <p>I have a table called groups as shown below :</p>
  
  <p><img src="https://i.stack.imgur.com/L8c5q.jpg" alt="enter image description here"></p>
  
  <p>After looking at the image above I think you might have understood that primary key and foreign key exist in the same table. I think this is what developers call cyclic reference.</p>
  
  <p>In MainWindow.xaml I have a DataGrid which contains three columns namely Group Name, Parent Name, Description. The xaml looks like :</p>
  
  <pre><code>&lt;Window .......&gt;
  
      &lt;Window.DataContext&gt;
          &lt;self:MainWindowViewModel /&gt;
      &lt;/Window.DataContext&gt;
  
      &lt;DataGrid ItemsSource="{Binding Groups}" TabIndex="1"&gt;
  
          &lt;DataGrid.Columns&gt;
  
              &lt;DataGridTemplateColumn Header="Group Name" Width="2*"&gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock Text="{Binding GroupName}"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBox Text="{Binding GroupName}" /&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
  
              &lt;DataGridTemplateColumn Header="Parent" Width="2*"&gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock Text="{Binding ParentID, Converter={StaticResource GroupIDToGroupNameConverter}}" /&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;ComboBox ItemsSource="{Binding DataContext.GroupsCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
                                    SelectedValue="{Binding ParentID}"
                                    SelectedValuePath="GroupID"
                                    DisplayMemberPath="GroupName"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
  
              &lt;DataGridTemplateColumn Header="Description" Width="2*"&gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock Text="{Binding Description}"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBox Text="{Binding Description}"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
  
          &lt;/power:PowerDataGrid.Columns&gt;
  
      &lt;/power:PowerDataGrid&gt;
  
  &lt;/Window&gt;
  </code></pre>
  
  <p>Now I have a ViewModel called MainWindowViewModel</p>
  
  <pre><code>public class MainWindowViewModel : INotifyPropertyChanged
  {
      public MainWindowViewModel()
      {
          SampleDBContext sampleDBContext = new SampleDBContext();
          Groups = new ObservableCollection&lt;Group&gt;();
          GroupsCollection = new ObservableCollection&lt;Group&gt;(from g in sampleDBContext.Groups select g);
      }
  
      private ObservableCollection&lt;Group&gt; _groups;
      public ObservableCollection&lt;Group&gt; Groups
      {
          get
          {
              return _groups;
          }
          set
          {
              _groups = value;
              OnPropertyChanged("Groups");
          }
      }
  
      private ObservableCollection&lt;Group&gt; _groupsCollection;
      public ObservableCollection&lt;Group&gt; GroupsCollection
      {
          get
          {
              return _groupsCollection;
          }
          set
          {
              _groupsCollection = value;
              OnPropertyChanged("GroupsCollection");
          }
      }
  
      #region INotifyPropertyChanged
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected void OnPropertyChanged(string propertryName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertryName));
          }
      }
  
      #endregion
  }
  </code></pre>
  
  <p>GroupIDToGroupName.cs //Converter</p>
  
  <pre><code>public class GroupIDToGroupName : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          if (value != null)
          {
              SampleDBContext sampleDBContext = new SampleDBContext();
              return (from g in sampleDBContext.Groups
                      where g.GroupID == (int)value
                      select g.GroupName).FirstOrDefault();
          }
          else
          {
              return "";
          }
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          SampleDBContext sampleDBContext = new SampleDBContext();
          return (from g in sampleDBContext.Groups
                  where g.GroupName == (string)value
                  select g.GroupID).FirstOrDefault();
      }
  }
  </code></pre>
  
  <p>In App.xaml :</p>
  
  <pre><code>&lt;self:GroupIDToGroupName x:Key="GroupIDToGroupNameConveerter" /&gt;
  </code></pre>
  
  <p><strong>My Case (Very similar to above sample):</strong></p>
  
  <p>I just want to use a Multi-Column ComboBox instead of simple ComboBox inside DataGrid.</p>
  
  <p>I have two tables : </p>
  
  <p><img src="https://i.stack.imgur.com/a3P3t.jpg" alt="enter image description here"></p>
  
  <p>Now I have set up my code exactly as the above mentioned code.</p>
  
  <p>I have added an extra class called GroupIDAndNameWithCorrespondingEffect like :</p>
  
  <pre><code>public class GroupIDAndNameWithCorrespondingEffect : INotifyPropertyChanged
  {
      private int _groupID;
      public int GroupID
      {
          get
          {
              return _groupID;
          }
  
          set
          {
              _groupID = value;
              OnPropertyChanged("GroupID");
          }
      }
  
      private string _groupName;
      public string GroupName
      {
          get
          {
              return _groupName;
          }
  
          set
          {
              _groupName = value;
              OnPropertyChanged("GroupName");
          }
      }
  
      private string _correspondingEffect;
      public string CorrespondingEffect
      {
          get
          {
              return _correspondingEffect;
          }
  
          set
          {
              _correspondingEffect = value;
              OnPropertyChanged("CorrespondingEffect");
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected void OnPropertyChanged(string propertyName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  
  <p>Changes in my ViewModel :</p>
  
  <p>I removed the property GroupsCollection and all its references and added a new property called GroupIDAndNamesWithCorrespondingEffects as below :</p>
  
  <pre><code>private ObservableCollection&lt;GroupIDAndNameWithCorrespondingEffect&gt; _groupIDAndNamesWithCorrespondingEffects;
  public ObservableCollection&lt;GroupIDAndNameWithCorrespondingEffect&gt; GroupIDAndNamesWithCorrespondingEffects
  {
      get
      {
          return _groupIDAndNamesWithCorrespondingEffects;
      }
      set
      {
          _groupIDAndNamesWithCorrespondingEffects = value;
          OnPropertyChanged("GroupIDAndNamesWithCorrespondingEffects");
      }
  }
  </code></pre>
  
  <p>And in the Constructor :</p>
  
  <pre><code>List&lt;GroupIDAndNameWithCorrespondingEffect&gt; _GroupIDAndNamesWithCorrespondingEffects = (
                                                                                                   from g in sampleDBContext.Groups
                                                                                                   select new GroupIDAndNameWithCorrespondingEffect
                                                                                                   {
                                                                                                       GroupID = g.GroupID,
                                                                                                       GroupName = g.GroupName,
                                                                                                       CorrespondingEffect = g.Effect.Effect1
                                                                                                   }
                                                                                               ).ToList();
  
              GroupIDAndNamesWithCorrespondingEffects
                  = new ObservableCollection&lt;GroupIDAndNameWithCorrespondingEffect&gt;(
                                                                                  _GroupIDAndNamesWithCorrespondingEffects.Where
                                                                                      (
                                                                                          u =&gt; !GetAllChildren(25)
                                                                                                  .Select(x =&gt; x.GroupID)
                                                                                                  .Contains(u.GroupID)
                                                                                      ).ToList()
                                                                              );
  </code></pre>
  
  <p>In my MainWindow.xaml I have added a resource as follows :</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;CollectionViewSource x:Key="GroupNamesWithCorrespondingEffectsCollection" Source="{Binding GroupIDAndNamesWithCorrespondingEffects}" /&gt;    
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>Inside Grid's Resources :</p>
  
  <pre><code>    &lt;Grid.Resources&gt;
  
          &lt;CompositeCollection x:Key="Items"&gt;
              &lt;ComboBoxItem IsEnabled="False" Background="#FF2A2A2A" Foreground="White"&gt;
                  &lt;Grid TextElement.FontWeight="Bold" &gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition SharedSizeGroup="A" /&gt;
                          &lt;ColumnDefinition Width="50" /&gt;
                          &lt;ColumnDefinition SharedSizeGroup="B" /&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
                      &lt;Grid.Children&gt;
                          &lt;TextBlock Grid.Column="0" Text="Group Name" /&gt;
                          &lt;TextBlock Grid.Column="2" Text="Effect" /&gt;
                      &lt;/Grid.Children&gt;
                  &lt;/Grid&gt;
              &lt;/ComboBoxItem&gt;
              &lt;CollectionContainer Collection="{Binding Source={StaticResource GroupNamesWithCorrespondingEffectsCollection}}" /&gt;
          &lt;/CompositeCollection&gt;
  
          &lt;DataTemplate DataType="{x:Type self:GroupIDAndNameWithCorrespondingEffect}"&gt;
              &lt;Grid&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition SharedSizeGroup="A" /&gt;
                      &lt;ColumnDefinition Width="50" /&gt;
                      &lt;ColumnDefinition SharedSizeGroup="B" /&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;Grid.Children&gt;
                      &lt;TextBlock Grid.Column="0" Text="{Binding GroupName}" /&gt;
                      &lt;TextBlock Grid.Column="2" Text="{Binding CorrespondingEffect}" /&gt;
                  &lt;/Grid.Children&gt;
              &lt;/Grid&gt;
          &lt;/DataTemplate&gt;
  
      &lt;/Grid.Resources&gt;
  </code></pre>
  
  <p>And I changed the ItemsSource of ComboBox to ItemsSource="{DynamicResource Items}".</p>
  
  <p><strong>Problems :</strong></p>
  
  <p>When I run the program ComboBox displays all the items correctly. Also, two columns with headers are displayed. Its working fine, but when I press <kbd>Enter</kbd> or <kbd>TAB</kbd>, then focus remains in the same cell and the comboBox's text displays namespace of GroupIDAndNameWithCorrespondingEffect </p>
  
  <p>Here is the image of Problem :</p>
  
  <p><img src="https://i.stack.imgur.com/ZcPn9.jpg" alt="enter image description here"></p>
  
  <p><strong>Sample:</strong></p>
  
  <p>Incase if somebody wants to check the sample then it is available <a href="https://drive.google.com/file/d/0B5WyqSALui0bU01iWDBlb09WRVE/edit?usp=sharing" rel="nofollow noreferrer">here</a>. And database files are available <a href="https://drive.google.com/file/d/0B5WyqSALui0bQzdWOWc4Q2tvY1E/edit?usp=sharing" rel="nofollow noreferrer">here</a>.</p>