locked
How to check which item in Gridview is in view RRS feed

  • Question

  • Hello,

    I am building a calendar and put the months into a GridView. How to get the current month Item in view from code behind? Kind of like the opposite of ScrollIntoView?

    Thanks.

    Wednesday, August 14, 2013 3:56 PM

Answers

  • Hi Helen.W ,

    According to your description, i guess you wonder such a function that while users scroll your gridview, you want to get the current month item in view from code behind without users select a item.

    If so, would you mind to share the display style of your gridview? Does your gridview display an item once? 

    If your gridview display the item regularly, I advise you to use ScrollViewer as a container and place your GridView in it. Then you could use ScrollViewer.ViewerChanged event to monitor if the GridView item in view is changed.

    XAML of Sample :

     <ScrollViewer x:Name="scroll" Width="100" Height="100" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" ViewChanged="scroll_ViewChanged">
                 <GridView x:Name="grid" ScrollViewer.HorizontalScrollMode="Auto" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" ItemClick="grid_ItemClick" IsItemClickEnabled="True" />
            </ScrollViewer>

    Code behind of Sample:

     private void scroll_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
            {
                ScrollViewer scr = (ScrollViewer)sender;
                double horoffset = scr.HorizontalOffset;//if your scrollviewer is display horizontally
                double veroffset = scr.VerticalOffset; //if your scrollviewer is display vertically
                //please calculate the relative displacement and calculate the index of items by (relative displacement/item's height or with)
            }

    If you are afraid only part of item is in the view, please use ScrollIntoView for help.

    If you need more help, please let me know!

    Best Regards!



    Thursday, August 15, 2013 11:10 AM
    Moderator
  • Hi Helen.W ,

    HorizontalOffset/VerticalOffset is the property of ScrollViewer. And HorizontalOffset/VerticalOffset is Relative displacement of the rolling on ScrollViewer. When you view a item per in ScrollViewer, you could get the index of items. Please set the item fill ScrollViewer. Then you could get the exact relative displacement.

    Please just have a try!

    Best Regards!

    Monday, August 19, 2013 3:40 AM
    Moderator

All replies

  • Hi Helen.W ,

    According to your description, i guess you wonder such a function that while users scroll your gridview, you want to get the current month item in view from code behind without users select a item.

    If so, would you mind to share the display style of your gridview? Does your gridview display an item once? 

    If your gridview display the item regularly, I advise you to use ScrollViewer as a container and place your GridView in it. Then you could use ScrollViewer.ViewerChanged event to monitor if the GridView item in view is changed.

    XAML of Sample :

     <ScrollViewer x:Name="scroll" Width="100" Height="100" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" ViewChanged="scroll_ViewChanged">
                 <GridView x:Name="grid" ScrollViewer.HorizontalScrollMode="Auto" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" ItemClick="grid_ItemClick" IsItemClickEnabled="True" />
            </ScrollViewer>

    Code behind of Sample:

     private void scroll_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
            {
                ScrollViewer scr = (ScrollViewer)sender;
                double horoffset = scr.HorizontalOffset;//if your scrollviewer is display horizontally
                double veroffset = scr.VerticalOffset; //if your scrollviewer is display vertically
                //please calculate the relative displacement and calculate the index of items by (relative displacement/item's height or with)
            }

    If you are afraid only part of item is in the view, please use ScrollIntoView for help.

    If you need more help, please let me know!

    Best Regards!



    Thursday, August 15, 2013 11:10 AM
    Moderator
  • Thanks Xiaoliang.

    But the statement you commented is really what I don't know. ScrollViewer has its own rules to displace certain number of rows per page. For example, it may show all months in one row, or show them in 2 rows. And I didn't see GridViewItem has a property HorizontalOffset/VerticalOffset showing how far it's from the edge of the parent ScrollViewer.

    Thanks for further explanation in advance.

    Sunday, August 18, 2013 7:38 AM
  • Hi Helen.W ,

    HorizontalOffset/VerticalOffset is the property of ScrollViewer. And HorizontalOffset/VerticalOffset is Relative displacement of the rolling on ScrollViewer. When you view a item per in ScrollViewer, you could get the index of items. Please set the item fill ScrollViewer. Then you could get the exact relative displacement.

    Please just have a try!

    Best Regards!

    Monday, August 19, 2013 3:40 AM
    Moderator