2011-07-27 115 views
2

我正在嘗試導航之間的2頁windows phone 7中的列表框項目

對於查看頁面有一個列表框,我選擇裏面的一個項目將導航到「deatils頁面」。

當我按下手機的返回鍵按鈕時,它會導航我從「詳細信息頁面」返回到「查看頁面」

但「查看頁面」中的列表框顯示所選項目突出顯示。

如何刷新「查看頁面」,使列表框不會顯示任何已被選中。

代碼爲我的鑰匙手機的 「詳細信息頁」 的後退按鈕:

private void PhoneApplicationPage_BackKeyPress(object sender, 
               System.ComponentModel.CancelEventArgs e) 
{ 

    string selectedFolderName = selectedFolderName2; 

    NavigationService.Navigate(
     new Uri(
      "/DisplaySchedule.xaml?=selectedFolderName2" + selectedFolderName, 
      UriKind.Relative)); 
} 

代碼爲我的導航頁「的ViewPage:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 

    base.OnNavigatedTo(e); 
    selectedFolderName = ""; 

    if (NavigationContext.QueryString.TryGetValue("selectedFolderName", out selectedFolderName)) 
     selectedFolderName1 = selectedFolderName; 


     IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 
     //For time 
     try 
     { 
      StreamReader readFileTime = new StreamReader(new IsolatedStorageFileStream(selectedFolderName1 + "\\time.Schedule", FileMode.Open, myStore)); 
      //For title 
      StreamReader readFileTitle = new StreamReader(new IsolatedStorageFileStream(selectedFolderName1 + "\\title.Schedule", FileMode.Open, myStore)); 
      //For category 
      StreamReader readFileCategory = new StreamReader(new IsolatedStorageFileStream(selectedFolderName1 + "\\category.Schedule", FileMode.Open, myStore)); 


      String timeText = readFileTime.ReadLine(); 
      timeSplit = timeText.Split(new char[] { '^' }); 

      String titleText = readFileTitle.ReadLine(); 
      titleSplit = titleText.Split(new char[] { '^' }); 

      String categoryText = readFileCategory.ReadLine(); 
      categorySplit = categoryText.Split(new char[] { '^' }); 
     } 
     catch (Exception) 
     { 
     } 

     //Define grid column, size 
     Grid schedule = new Grid(); 

     if (scheduleListBox.Items.Count == 0) 
     { 
      foreach (var time in timeSplit) 
      { 
       timeList = time; 

       //Column 1 to hold the time of the schedule 
       ColumnDefinition scheduleTimeColumn = new ColumnDefinition(); 
       GridLength timeGrid = new GridLength(110); 
       scheduleTimeColumn.Width = timeGrid; 
       schedule.ColumnDefinitions.Add(scheduleTimeColumn); 

       //Text block that show the time of the schedule 
       TextBlock timeTxtBlock = new TextBlock(); 
       timeTxtBlock.Text = time; 
       //Set the alarm label text block properties - margin, fontsize 
       timeTxtBlock.FontSize = 28; 
       timeTxtBlock.Margin = new Thickness(0, 20, 0, 0); 
       //Set the column that will hold the time of the schedule 
       Grid.SetColumn(timeTxtBlock, 0); 

       schedule.Children.Add(timeTxtBlock); 
      } 

      foreach (var title in titleSplit) 
      { 
       titleList = title; 

       //Column 2 to hold the title of the schedule 
       ColumnDefinition scheduleTitleColumn = new ColumnDefinition(); 
       GridLength titleGrid = new GridLength(500); 
       scheduleTitleColumn.Width = titleGrid; 
       schedule.ColumnDefinitions.Add(scheduleTitleColumn); 

       //Text block that show the title of the schedule 
       TextBlock titleTxtBlock = new TextBlock(); 

       if (title.Length > 10) 
       { 
        string strTitle = title.Substring(0, 10) + "...."; 
        titleTxtBlock.Text = strTitle; 
       } 
       else 
       { 
        titleTxtBlock.Text = title; 
       } 

       //Set the alarm label text block properties - margin, fontsize 
       titleTxtBlock.FontSize = 28; 
       titleTxtBlock.Margin = new Thickness(0, 20, 0, 0); 
       //Set the column that will hold the title of the schedule 
       Grid.SetColumn(titleTxtBlock, 1); 

       schedule.Children.Add(titleTxtBlock); 
      } 

      foreach (var category in categorySplit) 
      { 
       categoryList = category; 

       //Column 3 to hold the image category of the schedule 
       ColumnDefinition categoryImageColumn = new ColumnDefinition(); 
       GridLength catImgnGrid = new GridLength(70); 
       categoryImageColumn.Width = catImgnGrid; 
       schedule.ColumnDefinitions.Add(categoryImageColumn); 

       TextBlock categoryTxtBlock = new TextBlock(); 
       categoryTxtBlock.Text = category; 

       //set the category image and its properties - margin, width, height, name, background, font size 
       Image categoryImage = new Image(); 
       categoryImage.Margin = new Thickness(-50, 15, 0, 0); 
       categoryImage.Width = 50; 
       categoryImage.Height = 50; 
       if (category == "Priority") 
       { 
        categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/exclamination_mark.png", UriKind.Relative)); 
       } 
       else 
        if (category == "Favourite") 
        { 
         categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/star_full.png", UriKind.Relative)); 
        } 


       Grid.SetColumn(categoryImage, 2); 
       schedule.Children.Add(categoryImage); 
      } 

      scheduleListBox.Items.Add(schedule); 
     } 
    } 



    private void scheduleListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     //Get the value of selected index in scheduleListBox 
     int selectedIndexOfSchedule = scheduleListBox.SelectedIndex; 

     NavigationService.Navigate(new Uri("/ViewScheduleDetails.xaml?selectedIndexOfSchedule=" + selectedIndexOfSchedule + "&selectedFolderName1=" + selectedFolderName1, UriKind.Relative)); 
    } 

回答

0

簡單地取消選擇一個項目

myListBox.SelectedItem = -1; 
+0

嗯在哪裏把代碼? –

+0

navigatedTo事件的頂部? –

+0

嘗試但仍然相同 –

0

選擇一個項目後,只需刪除選項(參見下面的CAPS中的註釋)

private void scheduleListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     //Get the value of selected index in scheduleListBox 
     int selectedIndexOfSchedule = scheduleListBox.SelectedIndex; 

     // RESET THE SELECTED INDEX/ITEM 
     scheduleListBox.SelectedIndex = -1; 
     // OR 
     scheduleListBox.SelectedItem = null; 

     NavigationService.Navigate(new Uri("/ViewScheduleDetails.xaml?selectedIndexOfSchedule=" + selectedIndexOfSchedule + "&selectedFolderName1=" + selectedFolderName1, UriKind.Relative)); 
    } 
+0

嘗試。 但我想要的是將選定的值傳遞到「詳細信息頁面」。 然後,當我從「詳細信息頁面」導航到「查看頁面」,然後列表框變爲空... 試過你直接通過上面的代碼-1作爲「詳細信息頁面」的選定索引。 –

+0

@ben你不應該試圖訪問其他頁面的UIElement。取而代之的是在一個頁面上獲取所選對象的唯一標識符,然後在第二頁中查找相同的項目。當然,假設兩頁都可以訪問相同的數據。 –

相關問題