简体   繁体   English

WP7获取列表框项目文本

[英]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. 因此,在单击时,它会为列表框项中的文本设置一个字符串。

In a new DataBound App, changed the following method to see 3 ways of getting this: 在新的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM