title: Accessing data in a listbox
body: <p>Hey I need to access items in my listbox I've made in WPF. What I have right now is the user can add a new class object to ObservableCollection which the ListBox.ItemsSource is bound to. This class has data in it where that data is used in things like a combobox, a text box and other simple things like that. </p>
  
  <p>What i've made is really based off of one of the tut videos from windowsclient.net</p>
  
  <pre><code>    //Here's What I have so far
      &lt;ListBox x:Name="MyList" Width = "300" Height = 300"&gt;
           &lt;ListBox.ItemTemplate&gt;
                &lt;DataTemplate&gt;
                     &lt;StackPanel Orientation="Vertical"&gt;
                          &lt;TextBlock x:Name="ItemName" 
                           Text={Binding Path=PersonName}/&gt;
                          &lt;ComboBox x:Name="NumberOfRes" 
                           SelectedIndex="0"
                           SelectionChanged="NumberOfRes_SelectionChanged"
                           ItemsSource={Binding Path=ListOfNums}/&gt;
                     &lt;/StackPanel&gt;
                &lt;/DataTemplate&gt;
           &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
  
      //ListOfNums
      private ObservableCollection&lt;int&gt; _listofNums = 
                       new ObservableCollection&lt;int&gt;()
      { 1,2,3,4,5,6,7,8 };
      public ObservableCollection&lt;int&gt; ListOfNums
      {
           get{return _listOfNums;}
      }
  
      //class Resident
      public class Resident
      {
          public string personName {get;set;}
      }
  </code></pre>
  
  <p>So I have that working. I have a button that adds a new class object and each object has it's data set properly through data binding. So I can see my default personName when a new object is added and I have a comboBox filled with integers from ListOfNums.</p>
  
  <p>My propblem though is that I don't know how to access what these 'sub objects' (forgive me I'm new to WPF and don't know the proper term) are selecting. I want to know what my comboBox's SelectedItem is when a selectionChange event is raised or what my new personName is set to when the user types something into the textBox. So a user selects one of these ItemTemplate objects in the listBox and this item has a combobox and other UI controls that the user clicks. When the user clicks on one of these UI controls some data changes. </p>
  
  <p>I've tried <code>((Resident)MyList.SelectedItem).personName</code> and a couple other methods just to see if I can access the data and manipulate it. Now the funny thing is that my comboBox in the ItemTemplate has a selection changed event that is called when the selection is changed, but I can't access the data!</p>
  
  <p>Thanks in advance!</p>