title: Getting information out of a listbox windows phone 8
body: <p>i'm currently developing an app for windows phone 8, that grabs JSON data from the internet, parses it into a collection, binds this collection to a listbox and shows it. This works fine, and i do it like this:</p>
  
  <pre><code>void downloadData()
  {
      // Instance of a WebClient object
      WebClient downloader = new WebClient();
  
      // EventHandler for download String completed
      downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(downloaded);
  
      // AsyncDownloading of the Websitecontent and Encoding in UTF8
      downloader.Encoding = Encoding.UTF8;
      downloader.DownloadStringAsync(new Uri("http://scm1.hensgen.net:8181/cxf/plugins/mandantenmonitor/rs/list", UriKind.Absolute));
  }
  
  void downloaded(object sender, DownloadStringCompletedEventArgs e)
  {
      // tests wheter string is empty or not downloaded completely
      if (e.Result == null || e.Error != null)
      {
          MessageBox.Show("Error occured while downloading JSON-file from server");
      }
      else
      {
          // Deserialize if downloaded succeedes
          DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MandantenListeRoot));
  
          // Reads the e.Result string and writes it in UTF8 encoded into a MemoryStream and Cast it from type object to MandantenListeRoot
          MandantenListeRoot root = serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(e.Result))) as MandantenListeRoot;
          MandantenListe mandListe = root.mandantenListeDataMember;
  
          // Bind the downloaded Collection to our MandantenListBox-control Panel
          mandantenListeBox.ItemsSource = mandListe.MandantenCollection;
      }
  
  }
  </code></pre>
  
  <p>I want to parse and ID attribute of this collection to the next page in my up when i click on one list entry.
  Now i read about this a bunch on the tubes and if i understand it correctly i should simply be able to cast the sender object in the MouseButtonDown method and pass it onto the next page like this</p>
  
  <pre><code>  private void MandantenStackPanel_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
      {
          //MandantenListeMandant mandant = (sender as Button).DataContext as MandantenListeMandant
          // PhoneApplicationService.Current.State["MandantenNummer"] = mandant.MandantenNummer;
          NavigationService.Navigate(new Uri("/Vorlagen.xaml", UriKind.Relative));
  
      }
  </code></pre>
  
  <p>This does not seem work and if i read the debug information i get correctly the sender object is -1. The relevant XAML for my page looks like this:</p>
  
  <pre><code> &lt;ListBox x:Name="mandantenListeBox" Margin="0,0,0,0"&gt;
                  &lt;ListBox.ItemTemplate&gt;
                      &lt;DataTemplate &gt;
                          &lt;StackPanel Margin="0,10,0,10" Width="455" MouseLeftButtonDown="MandantenStackPanel_MouseLeftButtonDown"&gt;
                              &lt;TextBlock Text="{Binding MandantenNummer}" FontSize="24" TextWrapping="Wrap"/&gt;
                              &lt;TextBlock Text="{Binding MandantenBezeichnung}" FontSize="24" TextWrapping="Wrap"/&gt;
                          &lt;/StackPanel&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ListBox.ItemTemplate&gt;
              &lt;/ListBox&gt;
  </code></pre>
  
  <p>Thank you for your help, i really appreciate it.</p>