title: WPF: C# How to get selected value of Listbox?
body: <p>I am trying to fetch the selected value of the listbox. When I actually try <code>LstGroup1.SelectedItem</code> I get the value <code>{ BoothID = "4", BoothName = "HP" }</code> and even if i try to get the value from <code>LstGroup1.SelectedValue</code> the output is same. Now I want to fetch <code>BoothID</code> i.e. the expected output is <code>4</code> but i am unable to get so.</p>
  
  <p>My <code>ListBox</code> name is <code>LstGroup1</code>.</p>
  
  <pre><code>public List&lt;object&gt; BoothsInGroup1 { get; set; }
  // Inside the Constructor 
  BoothsInGroup1 = new List&lt;object&gt;();
  //After Fetching the value add 
  BoothsInGroup1.Add(new { BoothID = da["BoothID"].ToString(), BoothName = da["BoothName"].ToString() });
  
  //Now here I get 
  var Booth = (LstGroup1.SelectedItem);
  //Output is { BoothID = "4", BoothName = "HP" }
  
  // Expected Output 4
  </code></pre>
  
  <p>Suggest me how to do that.</p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <pre><code>public partial class VotingPanel : Window
  {
      public List&lt;object&gt; BoothsInGroup1 { get; set; }
      public VotingPanel()
      {
          InitializeComponent();
          DataContext = this;
          BoothsInGroup1 = new List&lt;object&gt;();
  
          //Connection is done
          SqlConnection con = new SqlConnection(ConnectionString);
          con.Open();
  
          FieldNameCmd.CommandText = "SELECT * FROM Booth b, BoothGroup bg where bg.GroupID=b.GroupID;";
          IDataReader da = FieldNameCmd.ExecuteReader();
          while (da.Read())
          {
              if (Group1Name.Text == da["GroupName"].ToString())
              { // Adds value to BoothsInGroup1
                  BoothsInGroup1.Add(new { BoothID = da["BoothID"].ToString(), BoothName = da["BoothName"].ToString() });
              }
          }
      }
      private void BtnVote_Click(object sender, RoutedEventArgs e) // on Click wanted to find the value of Selected list box
      {   
          if (LstGroup1.SelectedIndex &gt;= 0 &amp;&amp; LstGroup2.SelectedIndex &gt;= 0)
          {
              var Booth = (LstGroup1.SelectedItem);
              //Few things to do 
          }
      }
  }
  </code></pre>
  
  <h2>XAML</h2>
  
  <pre><code>&lt;ListBox Grid.Row="1"
           Name="LstGroup1"
           ItemsSource="{Binding BoothsInGroup1}"
           Margin="5,1"&gt;
      &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;Grid Margin="5"&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition /&gt;
                      &lt;ColumnDefinition /&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;TextBlock Text="{Binding BoothID}"
                             HorizontalAlignment="Center"
                             VerticalAlignment="Bottom"
                             FontSize="15"
                             FontWeight="ExtraBold"
                             Margin="5,3"
                             Grid.Column="1" /&gt;
                  &lt;TextBlock Text="{Binding BoothName}"
                             Grid.Column="1"
                             VerticalAlignment="Center"
                             FontWeight="ExtraBold"
                             FontSize="30"
                             Margin="15" /&gt;
  
              &lt;/Grid&gt;
          &lt;/DataTemplate&gt;
      &lt;/ListBox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>