Devi Prasad
Devi Prasad

Reputation: 839

ScrollIntoView property not working for gridview in windows 10 universal app

I tried this below code:

XAML Code:

<GridView x:Name="listgrid">
 <GridView.ItemTemplate>
   <DataTemplate>
     <StackPanel Margin="15,15,0,0">
       <Image Height="170" Width="170" Source="{Binding}"></Image>
     </StackPanel>
   </DataTemplate>
 </GridView.ItemTemplate>

Cs code:

for (int i = 1; i < 50; i++)
{
   list.Add("ms-appx:///Images/A-aa.jpg");
}
listgrid.ItemsSource = list;
listgrid.ScrollIntoView(listgrid.Items[30]);

I above code to scroll view to my selected item, but it's not showing any changes, i think i used this property in a wrong way any one please help me to scroll to gridview position.

Upvotes: 1

Views: 1093

Answers (3)

guest
guest

Reputation: 125

Try this

private void Gridview_Loaded(object sender, RoutedEventArgs e)
    {
        if (ShellPage.Current.SelectedRecItem != null)
        {
            this.gridview.SelectedItem = ShellPage.Current.SelectedRecItem;
            this.gridview.UpdateLayout();
            this.gridview.ScrollIntoView(ShellPage.Current.SelectedRecItem);
        }
    }

Upvotes: 0

Sunteen Wu
Sunteen Wu

Reputation: 10627

I have replied your same question in MSDN: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/d0a772b3-80b9-4a11-92a9-89963c29a52f/scrollintoview-property-not-working-for-gridview-in-windows-10-universal-app?forum=wpdevelop

You need to have something more to distinguish items, for example, give every image a name since items you bind to GridView are same, ScrollIntoView default find the first one.

And commonly you need to set a height property for the GridView.

For more complex requirements, there is a good thread you can reference: Windows 10 ScrollIntoView() is not scrolling to the items in the middle of a listview

Upvotes: 1

Andrii Krupka
Andrii Krupka

Reputation: 4306

Try to subscribe on Loaded event and call ScrollIntoView inside event handler:

listgrid.Loaded += Listgrid_Loaded;

....
private void Listgrid_Loaded(object sender, RoutedEventArgs e)
{
    listgrid.ScrollIntoView(listgrid.Items[30]);
}

Upvotes: 0

Related Questions