title: How to get a selected item in a list box with bindings?
body: <p>I have a wpf application in c# and my problem is now I have a Listbox with bindings and my question is how can I get the text from the textbox in the Listbox?</p>
  <p>My Listbox XAML:</p>
  <pre><code>            &lt;ListBox x:Name=&quot;chats_lb_friends&quot; ItemsSource=&quot;{Binding Friends_pic}&quot; Height=&quot;870&quot; Width=&quot;280&quot; Background=&quot;{x:Null}&quot; BorderBrush=&quot;{x:Null}&quot; Foreground=&quot;#FF535664&quot; FontSize=&quot;18&quot; FontFamily=&quot;/Nextopia Launcher v.2.0;component/fonts/#Mont DEMO&quot; FontWeight=&quot;Bold&quot; IsSynchronizedWithCurrentItem=&quot;False&quot; MinWidth=&quot;280&quot; SelectionChanged=&quot;chats_lb_friends_SelectionChanged_1&quot;&gt;
                  &lt;ListBox.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;StackPanel x:Name=&quot;chats_lb_friends_stackpanel&quot; Orientation=&quot;Horizontal&quot;&gt;
                              &lt;Ellipse x:Name=&quot;chats_lb_friends_img&quot; Width=&quot;25&quot; Height=&quot;25&quot; Stroke=&quot;{Binding status}&quot; StrokeThickness=&quot;2&quot;&gt;
                                  &lt;Ellipse.Fill&gt;
                                      &lt;ImageBrush Stretch=&quot;Fill&quot; ImageSource=&quot;{Binding imagePath}&quot;/&gt;
                                  &lt;/Ellipse.Fill&gt;
                              &lt;/Ellipse&gt;
                              &lt;TextBlock x:Name=&quot;chats_lb_friends_txt&quot; Text=&quot;{Binding username}&quot; Margin=&quot;5,0,0,0&quot; /&gt;
                          &lt;/StackPanel&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ListBox.ItemTemplate&gt;
              &lt;/ListBox&gt;
  </code></pre>
  <p>What do I want:</p>
  <pre><code>ListBoxItem MyItem = chats_lb_friends.SelectedItem as ListBoxItem;
  if(MyItem != null)
  {
      StackPanel sp = MyItem.Content as StackPanel;
      if(sp != null &amp;&amp; sp.Children.Count &gt; 0)
      {
          TextBlock textBlock = sp.Children[0] as TextBlock;
          if(textBlock != null)
          {
              string text = textBlock.Text;
          }
      }
  }
  </code></pre>
  <p>But this is not working have anyone an idea why?</p>
  <p>Thanks in the advance and sorry for my English</p>