WP7获取列表框项目文本

时间:2011-06-20 15:19:46

标签: windows-phone-7 listbox

有没有办法获取点击列表框项目的文本?

因此,单击它会将字符串设置为列表框项目中的文本。

1 个答案:

答案 0 :(得分:4)

在新的DataBound应用程序中,更改了以下方法以查看3种方法:

// 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;
}