locked
How to scrollIntoView my selected picture?

    Question

  • Hi:

    I'm working on a c# Xaml based app to display photos from my database.  I have an ItemsPage that shows Tiles that represent Photo events.  When the user clicks on an event. He is navigated to another ItemsPage, with tiles representing
    thumbnails of all the photos.

    When the user clicks on a photo he is navigate to another ItemsPage, but this time the tiles are sized such that only one photo is displayed on the screen.  My problem is that the ItemsPage always starts at the beginning, I can't seem to make it start with the selected photo;

    The NavigationParameter is correctly passing the picture id back.  With that, I am finding the picture in the collection within the NavigtationHelper_LoadState method:

    itemGridView.SelectedItem = foundPicture;
    itemGridView.ScrollIntoView(foundPicture);

    Seem like that should do it, but it's not.

    Any suggestions?


    Robotuner

    Friday, March 21, 2014 12:39 AM

Answers

  • Rob thanks, I finally realized that it had to be a not-yet initialized issue. so I dropped a loaded event handler on the page and realized that navigationHelper_LoadState is called before the OnLoaded event.    All I needed to do was to call ScrollIntoView from the onLoaded event handler.

    That was easy.

    Thanks.


    Robotuner

    • Marked as answer by Robotuner Friday, March 21, 2014 11:19 PM
    Friday, March 21, 2014 11:19 PM

All replies

  • ScrollIntoView should be correct, but surrounding code and environment may throw a wrench in things. When you call ScrollIntoView has the control finished initialising? Does calling UpdateLayout() help?

    Can you share a minimal sample demonstrating the problem on your OneDrive, along with repro steps and a description of the expected behavior and how it differs from the actual behavior?

    --Rob

    Friday, March 21, 2014 1:26 AM
    Owner
  • Thanks.  I'm guessing that it hasn't finished initializing.  I Threw an UpdateLayout call, but that didn't seem to change anything. Here is my method:

    private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
            {
                Tuple<PhotoEvtNdx2, PictureNdx2> ans = (Tuple<PhotoEvtNdx2, PictureNdx2>)e.NavigationParameter;
                PictureGroup pgroup = await PhotoDBDataSource.GetPictureItemAsync(ans.Item1);
                if (this.DataContext != null)
                {
                    (this.DataContext as ItemDetailVM).PEvent = ans.Item1;
                    (this.DataContext as ItemDetailVM).PGroup = pgroup;
                    // now we use the pictureNdx2 object to set the selected index.
                    PictureNdx2 foundndx = null;
                    foreach (PictureNdx2 pndx  in this.itemGridView.Items)
                    {
                        if (pndx.PictureId == ans.Item2.PictureId)
                        {
                            foundndx = pndx;
                            break;
                        }
                    
                    }
                    this.UpdateLayout();
                    if (foundndx != null)
                    {
                        this.itemGridView.SelectedItem = foundndx;
                        this.itemGridView.ScrollIntoView(foundndx);
    
                    }
                    this.UpdateLayout();
                }
            }


    Robotuner

    Friday, March 21, 2014 1:51 AM
  • Been playing with it today and have decided that ScrollIntoView doesn't work I this case, . . . as Rob implied, probably because the view hasn't fully initialized. My next strategy is to capture the position of the horizontal scroll bar and us that position when restoring the view. Does anyone know how to access the horizontal scroll bar in the GridView control? 

    Doesn't look like it is exposed.

    Thanks


    Robotuner

    Friday, March 21, 2014 10:45 PM
  • You can grovel through the GridView's template to find the scroll bar, but that won't help you here. You cannot automate it to move the view.

    Going on the not-yet-initialized theory I'd move the call to ScrollIntoView later after the view has a chance to initialize.

    Again, if you'd like more detailed help please upload a repro sample so somebody can follow along exactly what the code is doing (but probably not me as I'll be off of the forums for a bit).

    --Rob

    Friday, March 21, 2014 10:59 PM
    Owner
  • Rob thanks, I finally realized that it had to be a not-yet initialized issue. so I dropped a loaded event handler on the page and realized that navigationHelper_LoadState is called before the OnLoaded event.    All I needed to do was to call ScrollIntoView from the onLoaded event handler.

    That was easy.

    Thanks.


    Robotuner

    • Marked as answer by Robotuner Friday, March 21, 2014 11:19 PM
    Friday, March 21, 2014 11:19 PM