即将跳转到页面A,在页面A中有一个listbox,在跳转的时候,接收参数,自动选中listbox中的某项

/// <summary>

       /// 接收参数,定位当前选中项

       /// </summary> 

       protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs args)

       {

           IDictionary<string, string> parameters = this.NavigationContext.QueryString;

 

           if (parameters.ContainsKey("Id"))

           {

               string id = parameters["Id"];//图片Id

 

               int index = 0;

               for (int i = 0; i < imglist1.Items.Count; i++)

               {

 //ImgInfoBase为listbox的Item项; imglist1.ItemsSource=(List<ImgInfoBase>) ...

                   ImgInfoBase item = (ImgInfoBase)imglist1.Items[i];

                   if (item.Id == id)

                   {

                       index = i;

                       break;

                   }

               }

 

               if (imglist1.Items.Count > index)

               {

                   UpdateLayout();

                   imglist1.SelectedIndex = index;//使listbox滚动到指定的位置项上

 

                   //imglist1.ScrollIntoView(imglist1.Items[index]);

               }

           }

 

           base.OnNavigatedTo(args);

       }

05-08 14:55