user701025
user701025

Reputation:

WP7 Get listbox item text

Is there a way to get the text of a clicked listbox item?

So on click it sets a string to the text in the list box item.

Upvotes: 0

Views: 2666

Answers (1)

Matt Lacey
Matt Lacey

Reputation: 65556

In a new DataBound App, changed the following method to see 3 ways of getting this:

// Handle selection changed on ListBox
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // If selected index is -1 (no selection) do nothing
    if (MainListBox.SelectedIndex == -1)
        return;

    var string1 = ((sender as ListBox).SelectedItem as ItemViewModel).LineOne;
    var string2 = (MainListBox.SelectedItem as ItemViewModel).LineOne;
    var string3 = (e.AddedItems[0] as ItemViewModel).LineOne;

    // Navigate to the new page
    NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));

    // Reset selected index to -1 (no selection)
    MainListBox.SelectedIndex = -1;
}

Upvotes: 3

Related Questions