我试图获取所选项目的值并将其存储到变量中。
然后将列表框的选定索引重置为-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;
}
答案 0 :(得分:2)
您可以添加一项检查,以查看该值是否为-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;
}
}