title: How to access ListBox dynamically-created-items' properties from code-behind?
body: <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="WpfApp_ListBoxTest.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300"&gt;
      &lt;Grid&gt;
    &lt;ListBox Name="lb" Margin="0,0,0,70"&gt;&lt;/ListBox&gt;
    &lt;Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,41" Name="btnAdd" VerticalAlignment="Bottom" Content="Add item" Width="75" Click="btnAdd_Click"&gt;&lt;/Button&gt;
    &lt;TextBox Height="23" Margin="93,0,12,41" Name="txtInput" VerticalAlignment="Bottom" /&gt;
    &lt;Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,12" Name="btnGet" VerticalAlignment="Bottom" Content="Get value" Width="75" Click="btnGet_Click"&gt;&lt;/Button&gt;
    &lt;TextBox Height="23" Margin="93,0,12,12" Name="txtReturn" VerticalAlignment="Bottom" IsReadOnly="True" /&gt;
   &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Csharp:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  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;
  using System.Xml;
  
  namespace WpfApp_ListBoxTest
  {
   /// &lt;summary&gt;
   /// Interaction logic for Window1.xaml
   /// &lt;/summary&gt;
   public partial class Window1 : Window
   {
    public Window1()
    {
     InitializeComponent();
    }
  
    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
     TextBox txt = new TextBox();
     txt.Width = 200;
     txt.Text = txtInput.Text;
     lb.Items.Add(txt);
    }
  
    private void btnGet_Click(object sender, RoutedEventArgs e)
    {
     // What do I need to write here to get the value of the Text property of the selected TextBox?
  
    }
   }
  }
  </code></pre>
  
  <p>And screenshot: (Sorry I'm not allowed to post picture directly)
  <a href="http://i825.photobucket.com/albums/zz180/mGlushed/get_listbox_item_property.png" rel="nofollow noreferrer">http://i825.photobucket.com/albums/zz180/mGlushed/get_listbox_item_property.png</a></p>
  
  <p>(In the picture above, I want to get the value "b" when I click the "Get value" button.)</p>
  
  <p>I would like to know if there is a simple way to achieve this.</p>
  
  <p>I'm new to WPF, so I only know to do this the long way, which is: Create an array. Everytime a new TextBox is created, add it into the array. Then access the TextBox'es through the array. But that doesn't sound very optimal, I think.</p>