title: Retrieve the string value of a WPF ComboBox’s selected item in the code behind
body: <p>I’m having trouble retrieving the string value of a WPF ComboBox’s selected item in the code behind.</p>
  
  <p>I have set up three different types of array which provide the ItemSources for three ComboBoxes.  I’ve bound each ComboBox selected item to a property of a class called DataBase.  I verify the binding works by echoing the value of each DataBase property back to a TextBox.</p>
  
  <p>In the code behind I want to retrieve the string value of each ComboBox’s selected item.
  I can do this for the systemComboBox where SelectedItem and SelectedValue both return the string value of the selected item.</p>
  
  <p>I can’t get it to work for the oneDComboBox where SelectedItem and SelectedValue return “ComboBoxes.OneD” or for the twoDComboBox which correctly returns the SelectedValue but returns “ComboBoxes.TwoD” as the value for the SelectedItem</p>
  
  <p>Can anyone tell me how to get the string value of the oneDComboBox and twoDComboBox selected items?</p>
  
  <p>In Visual Studio I’ve set the Output Type of the code below Assembly to “Console Application” so I can write to the console.</p>
  
  <p>WPF</p>
  
  <pre><code> &lt;Window x:Class="ComboBoxes.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:ComboBoxes"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Width="1200" Height="450"
          mc:Ignorable="d"&gt;
  
      &lt;Window.Resources&gt;
          &lt;x:Array x:Key="OneDArray" Type="{x:Type local:OneD}"&gt;
              &lt;local:OneD OneDName="OneD-0" /&gt;
              &lt;local:OneD OneDName="OneD-1" /&gt;
              &lt;local:OneD OneDName="OneD-2" /&gt;
              &lt;local:OneD OneDName="OneD-3" /&gt;
              &lt;local:OneD OneDName="OneD-4" /&gt;
          &lt;/x:Array&gt;
  
          &lt;x:Array x:Key="TwoDArray" Type="{x:Type local:TwoD}"&gt;
              &lt;local:TwoD TwoDName="TwoD-0" TwoDNumber="0" /&gt;
              &lt;local:TwoD TwoDName="TwoD-1" TwoDNumber="1" /&gt;
              &lt;local:TwoD TwoDName="TwoD-2" TwoDNumber="2" /&gt;
              &lt;local:TwoD TwoDName="TwoD-3" TwoDNumber="3" /&gt;
              &lt;local:TwoD TwoDName="TwoD-4" TwoDNumber="4" /&gt;
          &lt;/x:Array&gt;
  
          &lt;x:Array x:Key="SystemStringArray" Type="sys:String"&gt;
              &lt;sys:String&gt;SystemString-0&lt;/sys:String&gt;
              &lt;sys:String&gt;SystemString-1&lt;/sys:String&gt;
              &lt;sys:String&gt;SystemString-2&lt;/sys:String&gt;
              &lt;sys:String&gt;SystemString-3&lt;/sys:String&gt;
              &lt;sys:String&gt;SystemString-4&lt;/sys:String&gt;
          &lt;/x:Array&gt;
  
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;Grid.Resources&gt;
              &lt;local:DataBase x:Key="dataBase" /&gt;
          &lt;/Grid.Resources&gt;
          &lt;Grid.DataContext&gt;
              &lt;Binding Source="{StaticResource dataBase}" /&gt;
          &lt;/Grid.DataContext&gt;
  
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="10" /&gt;
              &lt;RowDefinition Height="auto" /&gt;
              &lt;RowDefinition Height="20" /&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="10" /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;Grid Grid.Row="1" ShowGridLines="True"&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="20" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="40" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="40" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="*" /&gt;
                  &lt;ColumnDefinition Width="20" /&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;Label Grid.Row="1" Grid.Column="1" Content="OneDComboBox:" HorizontalAlignment="Right"/&gt;
              &lt;ComboBox x:Name="oneDComboBox" Grid.Row="1" Grid.Column="2" Width="120" DisplayMemberPath="OneDName"
                        ItemsSource="{StaticResource OneDArray}"
                        SelectedItem="{Binding Path=DataBaseOneDName, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
                        SelectionChanged="ComboBox_SelectionChanged"
                        /&gt;
              &lt;Label Grid.Row="1" Grid.Column="4" Content="SystemComboBox:" HorizontalAlignment="Right"/&gt;
              &lt;ComboBox x:Name="systemComboBox" Grid.Row="1" Grid.Column="5" Width="120"
                        ItemsSource="{StaticResource SystemStringArray}"
                        SelectedItem="{Binding Path=DataBaseSystemString, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
                        SelectionChanged="ComboBox_SelectionChanged"                      
                        /&gt;
              &lt;Label Grid.Row="1" Grid.Column="7" Content="TwoDComboBox:" HorizontalAlignment="Right"/&gt;
              &lt;ComboBox x:Name="twoDComboBox" Grid.Row="1" Grid.Column="8" Width="120" DisplayMemberPath="TwoDName"
                        ItemsSource="{Binding Source={StaticResource TwoDArray}}"
                        SelectedValue="{Binding Path=DataBaseTwoDNumber, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
                        SelectedValuePath="TwoDNumber" SelectionChanged="ComboBox_SelectionChanged"
                        /&gt;
          &lt;/Grid&gt;
          &lt;Grid Grid.Row="3" ShowGridLines="True"&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="20" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="40" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="40" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="120" /&gt;
                  &lt;ColumnDefinition Width="*" /&gt;
                  &lt;ColumnDefinition Width="20" /&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;Label Grid.Row="2" Grid.Column="1" Content="BoundOneDName:" HorizontalAlignment="Right"/&gt;
              &lt;TextBox x:Name="oneDComboBoxEcho" Grid.Row="3" Grid.Column="2" Width="120"
                       Text="{Binding Path=DataBaseOneDName, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
                       TextWrapping="Wrap" /&gt;
              &lt;Label Grid.Row="2" Grid.Column="4" Content="BoundSystemString:" HorizontalAlignment="Right"/&gt;
              &lt;TextBox x:Name="systemComboBoxEcho" Grid.Row="5" Grid.Column="5" Width="120"
                       Text="{Binding Path=DataBaseSystemString, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
                       TextWrapping="Wrap" /&gt;
              &lt;Label Grid.Row="2" Grid.Column="7" Content="BoundTwoDNumber:" HorizontalAlignment="Right"/&gt;
              &lt;TextBox x:Name="twoDComboBoxEcho" Grid.Row="5" Grid.Column="8" Width="120"
                       Text="{Binding Path=DataBaseTwoDNumber, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
                       TextWrapping="Wrap" /&gt;
  
          &lt;/Grid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;   
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>using System;
  using System.ComponentModel;
  using System.Windows;
  using System.Windows.Controls;
  
  
  namespace ComboBoxes
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// 
      /// The code below reflects the suggestions made by:
      /// 1  Ben Broadley (to use e.AddedItems[0]) and Benny (To look in the ItemsSource object) in the responses to https://stackoverflow.com/questions/4351603/get-selected-value-from-combo-box-in-c-sharp-wpf.
      /// 2  Adam Nathan in the FAQ box on page 266 of his book "WPF 4.5 Unleashed".
      /// 
      /// 
      /// 
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          //public MainWindow mainWindow;
          DataBase dataBase;
          public MainWindow()
          {
              InitializeComponent();
              dataBase = new DataBase();
              //DataContext = this;
          }
          private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              if (e.AddedItems.Count &gt; 0)                                                 //  Test to ensure that an item has been selected.
               {
                      ComboBox sourceComboBox = (ComboBox)sender;
                      int selectedIndex = -1;
                      string selectedItem = "???";
                      string selectedValue = "???";
                      string displayMemberPath = "???";
                      if (sourceComboBox.Name == "oneDComboBox")
                      {
                      //  Because this ComboBox.ItemsSource is bound to an array of types the selected value has to be unbundled from the selected type.  
                      if (oneDComboBox.SelectedValue is OneD)                                 //  Verify ComboBox.ItemsSource is bound to what we think its bound to.
                          {
                              selectedIndex = sourceComboBox.SelectedIndex;
                              selectedItem = sourceComboBox.SelectedItem.ToString();
                              selectedValue = (oneDComboBox.SelectedValue as OneD).OneDName;
                              displayMemberPath = sourceComboBox.DisplayMemberPath.ToString();
                          }
                      }
                      else
                      if (sourceComboBox.Name == "systemComboBox")
                      {
                          selectedIndex = sourceComboBox.SelectedIndex;
                          selectedItem = sourceComboBox.SelectedItem.ToString();
                          selectedValue = sourceComboBox.SelectedValue.ToString();
                          displayMemberPath = sourceComboBox.DisplayMemberPath.ToString();
                      }
                      else
                      if (sourceComboBox.Name == "twoDComboBox")
                      {
                      //  Because this ComboBox.ItemsSource is bound to an array of types the selected value has to be unbundled from the selected type.  
                      if (e.AddedItems[0] is TwoD)                                 //  Verify ComboBox.ItemsSource is bound to what we think its bound to.
                          {
                              selectedIndex = sourceComboBox.SelectedIndex;
                              selectedItem = sourceComboBox.SelectedItem.ToString();
                              selectedValue = (e.AddedItems[0] as TwoD).TwoDName;
                              //selectedValue = (twoDComboBox.SelectedValue as TwoD).TwoDName;  //  Why doesn't this work?  It works for oneDComboBox.
                          displayMemberPath = sourceComboBox.DisplayMemberPath.ToString();
                          }
                      }
                  Console.WriteLine($"\nComboBox Name = {sourceComboBox.Name}");
                  Console.WriteLine($"Selected Index = {selectedIndex}  Selected Item = {selectedItem}  Selected Value = {selectedValue}  DisplayMemberPath = {displayMemberPath}");
               }
          }
      }
      public partial class DataBase : INotifyPropertyChanged
      {
          private string _dataBaseOneDName = "OneDArray";
          public string DataBaseOneDName
          {
              get { return _dataBaseOneDName; }
              set
              {
                  if (_dataBaseOneDName != value)
                  {
                      _dataBaseOneDName = value;
                      NotifyPropertyChanged("DataBaseOneDName");
                  }
              }
          }
  
  
          private string _dataBaseSystemString = "System_String";
  
          public string DataBaseSystemString                         // String property used in binding examples.
          {
              get { return _dataBaseSystemString; }
              set
              {
                  if (_dataBaseSystemString != value)
                  {
                      _dataBaseSystemString = value;
                      NotifyPropertyChanged("DataBaseSystemString");
                  }
              }
          }
  
          private int _dataBaseTwoDNumber = 99;
  
  
          public int DataBaseTwoDNumber
          {
              get { return _dataBaseTwoDNumber; }
              set
              {
                  if (_dataBaseTwoDNumber != value)
                  {
                      _dataBaseTwoDNumber = value;
                      NotifyPropertyChanged("DataBaseTwoDNumber");
                  }
              }
          }                           // Int property used in binding examples.
  
  
          #region INotifyPropertyChanged Members
  
          /// Need to implement this interface in order to get data binding
          /// to work properly.
          private void NotifyPropertyChanged(string propertyName)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          #endregion
      }
  
  
      public partial class OneD                           // This class supports the "OneDArray" in the XAML.
      {
          public string OneDName { get; set; }
      }
      public partial class TwoD                         // This class supports the "TwoDArray" in the XAML.
      {
          public int TwoDNumber { get; set; }
          public string TwoDName { get; set; }
      }
  
  }
  </code></pre>