title: C#/WPF/xaml ComboBox populate with text and value using a datatable
body: <p>My Goal is to have a user select an item from a combobox and then use the associated value in a variable.</p>
  
  <p>Below is the code I have created, it populates the combobox with item from a datatable, but I'm unable to figure out how to populate the combobox with the value also and apply the value to a variable for later use.</p>
  
  <p>Yes.. I'm very new to C#/WPF/XAML</p>
  
  <h2>#MainWindow.xaml</h2>
  
  <pre><code>&lt;Window x:Class="ComboBoxFruitwValue.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;StackPanel Orientation="Vertical"&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;Label Content="Fruit:" VerticalContentAlignment="Center" 
          VerticalAlignment="Center"&gt;&lt;/Label&gt;
          &lt;ComboBox x:Name="cbxFruitDt" Width="300" Margin="15,0,0,0" VerticalAlignment="Center" 
          VerticalContentAlignment="Center" SelectionChanged="cbxFruitDt_SelectionChanged" &gt;
  
          &lt;/ComboBox&gt;
  
          &lt;/StackPanel&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBox x:Name="txt1" Width="200" /&gt;
  
          &lt;/StackPanel&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBox x:Name="txt2" Width="200" /&gt;
  
          &lt;/StackPanel&gt;
  
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBox x:Name="txt3" Width="200" /&gt;
  
          &lt;/StackPanel&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBox x:Name="txt4" Width="200" /&gt;
  
          &lt;/StackPanel&gt;
      &lt;/StackPanel&gt;
  
      &lt;/Grid&gt;
  
  &lt;/Window&gt;
  </code></pre>
  
  <h1>MainWindow.xaml.cs</h1>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  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 ComboBoxFruitwValue
  {
  /// &lt;summary&gt;
  /// Interaction logic for MainWindow.xaml
  /// &lt;/summary&gt;
  public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
          fill_cbxfruit();
      }
      void fill_cbxfruit()
      {
          using (DataTable dtFruit = new DataTable("fruits"))
          {
              dtFruit.Columns.Add("ID", typeof(int));
              dtFruit.PrimaryKey = new DataColumn[] { dtFruit.Columns["ID"] };
              dtFruit.Columns.Add("FruitCode", typeof(string));
              dtFruit.Columns.Add("FruitDescription", typeof(string));
              dtFruit.Rows.Add(1, "AP", "Apple");
              dtFruit.Rows.Add(2, "OR", "Orange");
              dtFruit.Rows.Add(3, "PE", "Pear");
              dtFruit.Rows.Add(4, "MA", "Mango");
              dtFruit.Rows.Add(5, "PA", "PineApple");
              dtFruit.Rows.Add(6, "GR", "Grapes");
              dtFruit.Rows.Add(7, "CH", "Cherries");
              dtFruit.Rows.Add(8, "SB", "Strawberries");
              dtFruit.Rows.Add(9, "BA", "Bananna");
  
              for (int i = 0; i &lt; dtFruit.Rows.Count; i++)
              {
               cbxFruitDt.Items.Add(dtFruit.Rows[i]["FruitDescription"]);
              }
  
  
  
          }
      }
  
      private void cbxFruitDt_SelectionChanged(object sender,        SelectionChangedEventArgs e)
      {
  
          txt1.Text = cbxFruitDt.SelectedIndex.ToString();
          txt2.Text = cbxFruitDt.SelectedItem.ToString();
          txt3.Text = cbxFruitDt.SelectedValuePath;
          //txt4.Text = cbxPccDt.DisplayMemberPath;
          txt4.Text = cbxFruitDt.Text;
      }
  }
  }
  </code></pre>