title: Selected Value in Multi-Column ComboBox
body: <p>I have a table named Groups in my Database as follows:</p>
  
  <pre><code>Groups
    |--GroupID
    |--GroupName
    |--ParentID
    |--NatureOfGroupID
    |--EffectID
  </code></pre>
  
  <p>Here is the relationship diagram for above table:</p>
  
  <p><img src="https://i.stack.imgur.com/lhgqM.jpg" alt="enter image description here"></p>
  
  <p>I have a combobox in a window in which I have two columns. Here is the xaml:</p>
  
  <pre><code>&lt;ComboBox ItemsSource="{Binding DataContext.GroupNamesWithCorrespondingEffects, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"
            SelectedValue="{Binding ParentID}" SelectedValuePath="GroupID"&gt;
      &lt;ComboBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;TextBlock Text="{Binding GroupName}" Width="200" /&gt;
                  &lt;TextBlock Text="{Binding CorrespondingEffect}" /&gt;
              &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
      &lt;/ComboBox.ItemTemplate&gt;
  &lt;/ComboBox&gt;
  </code></pre>
  
  <p>Actually i want to show GroupName and its corresponding Effect but when user selects any Item then I want to get SelectedValue as ParentID.</p>
  
  <p>Here is the implementation of GroupNameWithCorrespondingEffect.cs</p>
  
  <pre><code>public class GroupNameWithCorrespondingEffect : MainViewModel
  {
      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");
          } 
      }
  }
  </code></pre>
  
  <p>Here is the code for groupsViewModel.cs</p>
  
  <pre><code>public class GroupsViewModel : MainViewModel
  {
      public GroupsViewModel()
      {
  
          using (Entities db = new Entities())
          {
              GroupNamesWithCorrespondingEffects = (from g in db.Groups
                                                    select new GroupNameWithCorrespondingEffect
                                                    {
                                                        GroupName = g.GroupName,
                                                        CorrespondingEffect = g.Master_Effects.Effect
                                                    }).ToList();
  
              NaturesOfGroup = (from m in db.Master_NatureOfGroup
                                select m).ToList();
          }
  
          SaveChangesCommand = new RelayCommand(SaveGroup);
  
          CurrentGroup = new Group();
      }
  
      private Group _currentGroup;
      public Group CurrentGroup
      {
          get
          {
              return _currentGroup;
          }
  
          set
          {
              _currentGroup = value;
              OnPropertyChanged("CurrentGroup");
          }
      }
  
      private List&lt;GroupNameWithCorrespondingEffect&gt; _groupNamesWithCorrespondingEffects;
      public List&lt;GroupNameWithCorrespondingEffect&gt; GroupNamesWithCorrespondingEffects
      {
          get
          {
              return _groupNamesWithCorrespondingEffects;
          }
          set
          {
              _groupNamesWithCorrespondingEffects = value;
              OnPropertyChanged("GroupNamesWithCorrespondingEffects");
          }
      }
  
      private List&lt;Master_NatureOfGroup&gt; _naturesOfGroup;
      public List&lt;Master_NatureOfGroup&gt; NaturesOfGroup
      {
          get
          {
              return _naturesOfGroup;
          }
          set
          {
              _naturesOfGroup = value;
              OnPropertyChanged("NaturesOfGroup");
          }
      }
  
      public ICommand SaveChangesCommand { get; set; }
  
      private void SaveGroup(object obj)
      {
          Group cGroup = new Group()
          {
              GroupName = CurrentGroup.GroupName,
              **ParentID = CurrentGroup.ParentID,**
              NatureOfGroupID = CurrentGroup.NatureOfGroupID,
              EffectID = CurrentGroup.EffectID
          };
  
          using (Entities db = new Entities())
          {
              db.Groups.Add(cGroup);
              db.SaveChanges();
  
              GroupNamesWithCorrespondingEffects = (from g in db.Groups
                                                    select new GroupNameWithCorrespondingEffect
                                                    {
                                                        GroupName = g.GroupName,
                                                        CorrespondingEffect = g.Master_Effects.Effect
                                                    }).ToList();
  
          }
      }
  }
  </code></pre>
  
  <p>When I debug and put a breakpoint on the line marked with ** .... ** I always get CurrentGroup.ParentID = null.</p>
  
  <p><strong>Update:</strong></p>
  
  <pre><code>&lt;Page.DataContext&gt;
      &lt;vm:GroupsViewModel /&gt;
  &lt;/Page.DataContext&gt;
  
  &lt;Grid DataContext="{Binding CurrentGroup}"&gt;
  
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="*" /&gt;
          &lt;RowDefinition Height="Auto" /&gt;
          &lt;RowDefinition Height="Auto" /&gt;
          &lt;RowDefinition Height="Auto" /&gt;
          &lt;RowDefinition Height="*" /&gt;
          &lt;RowDefinition Height="Auto" /&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="*" /&gt;
          &lt;ColumnDefinition Width="Auto" /&gt;
          &lt;ColumnDefinition Width="0.6*" /&gt;
          &lt;ColumnDefinition Width="0.6*" /&gt;
          &lt;ColumnDefinition Width="*" /&gt;
      &lt;/Grid.ColumnDefinitions&gt;
  
      &lt;TextBlock Grid.Row="1" Grid.Column="1" Text="Name" HorizontalAlignment="Left"/&gt;
      &lt;TextBlock Grid.Row="1" Grid.Column="1" Text=" : " HorizontalAlignment="Right"/&gt;
      &lt;TextBox Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Text="{Binding GroupName}"/&gt;
  
      &lt;TextBlock Grid.Row="2" Grid.Column="1" Text="Under" HorizontalAlignment="Left"/&gt;
      &lt;TextBlock Grid.Row="2" Grid.Column="1" Text=" : " HorizontalAlignment="Right"/&gt;
      &lt;ComboBox x:Name="cbUnder" Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" 
                ItemsSource="{Binding DataContext.GroupNamesWithCorrespondingEffects, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"
                SelectedValue="{Binding ParentID}" SelectedValuePath="GroupID"&gt;
          &lt;ComboBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;StackPanel Orientation="Horizontal"&gt;
                      &lt;TextBlock Text="{Binding GroupName}" Width="200" /&gt;
                      &lt;TextBlock Text="{Binding CorrespondingEffect}" /&gt;
                  &lt;/StackPanel&gt;
              &lt;/DataTemplate&gt;
          &lt;/ComboBox.ItemTemplate&gt;
      &lt;/ComboBox&gt;
  
      &lt;TextBlock Grid.Row="3" Grid.Column="1" Text="Nature of Group" HorizontalAlignment="Left" Visibility="{Binding Visibility, ElementName=cbNature}"/&gt;
      &lt;TextBlock Grid.Row="3" Grid.Column="1" Text=" : " HorizontalAlignment="Right" Visibility="{Binding Visibility, ElementName=cbNature}"/&gt;
      &lt;ComboBox x:Name="cbNature" Grid.Row="3" Grid.Column="2" Visibility="{Binding SelectedIndex, Converter={StaticResource selectedIndexToVisibilityConverter}, ElementName=cbUnder}"
                ItemsSource="{Binding DataContext.NaturesOfGroup, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" DisplayMemberPath="Nature"
                SelectedValue="{Binding NatureOfGroupID}" SelectedValuePath="NatureOfGroupID"/&gt;
  
      &lt;StackPanel Grid.Row="5" Grid.Column="4" Orientation="Horizontal"&gt;
          &lt;Button Content="Save" Command="{Binding DataContext.SaveChangesCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"/&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>