有没有办法获取点击列表框项目的文本?
因此,单击它会将字符串设置为列表框项目中的文本。
答案 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;
}