在c# windows phone 7中获取listbox的选定值并重置选定的索引值

本文关键字:索引值 listbox windows phone 获取 | 更新日期: 2023-09-27 18:04:21

我正在尝试获取所选项目的值并将其存储到一个变量中。

然后将列表框的选定索引重置为-1,这样当我导航回该页时,列表框将不会显示任何先前选择的内容。下面是我的代码:

但是当选择的索引是重置为-1它将保持有一个错误在sortedTimeListBox.Items[selectedIndexOfSchedule].ToString();因为selectedIndexOfSchedule变成了-1

我想要的是只是获取值并导航到下一页。索引-1只是重置列表框的选定值。

我该怎么做?

private void scheduleListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
        //Get the value of selected index in scheduleListBox
        int selectedIndexOfSchedule = scheduleListBox.SelectedIndex;
        if (sortedSelectedValue.Text == "")
        {
            string selectedValueText = sortedTimeListBox.Items[selectedIndexOfSchedule].ToString();
            MessageBox.Show("selectedValueText : " + sortedSelectedValue.Text);
        }
        else
        {
            MessageBox.Show("Empty");
        }
        NavigationService.Navigate(new Uri("/ViewScheduleDetails.xaml?selectedIndexOfSchedule=" + selectedIndexOfSchedule + "&selectedFolderName1=" + fullFolderName + "&passToDelete=" + selectedFolderName, UriKind.Relative));
        scheduleListBox.SelectedIndex = -1;
}

在c# windows phone 7中获取listbox的选定值并重置选定的索引值

您可以添加一个检查,查看值是否为-1

private void scheduleListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
    //Get the value of selected index in scheduleListBox
    int selectedIndexOfSchedule = scheduleListBox.SelectedIndex;
    if(selectedIndexOfSchedule != -1)
    {
        if (sortedSelectedValue.Text == "")
        {
            string selectedValueText = sortedTimeListBox.Items[selectedIndexOfSchedule].ToString();
            MessageBox.Show("selectedValueText : " + sortedSelectedValue.Text);
        }
        else
        {
            MessageBox.Show("Empty");
        }
        NavigationService.Navigate(new Uri("/ViewScheduleDetails.xaml?selectedIndexOfSchedule=" + selectedIndexOfSchedule + "&selectedFolderName1=" + fullFolderName + "&passToDelete=" + selectedFolderName, UriKind.Relative));
        scheduleListBox.SelectedIndex = -1;
    }
}