locked
Get first Visible Item of ListView and Select That Item

    Question

  • Hi 

    I have a listView with a list of times, When a time is selected it shows on the screen that time. I want to make it so as the user scrolls down the list the first Item will be selected. 

    I know how to programatically select items in the list. I just dont know how to get the first visible Item of the list. 
    Since 8.1 doesnt have the method listView.TopItem() (Would have made life much easier ). 

    Im currently trying to make a boundary area which will go through the items and select the first on that is in the area. But How Would I call that method?
    To call the method I assumed it would be whenever the list Visible Items changed or when it was scrolled but I dont know which event would call them.

    Any help would be good :) 
    Thanks
    Wednesday, February 26, 2014 10:31 PM

Answers

  • Hi,

    In windows store app C#, there is no direct way to get the listview first visible item when scroll. But in WinJS, it has ListView.indexOfFirstVisible property which can get or set the first visible item in Listview.

    But I think you can create a similar way to implement such behavior, ListView or GridView contains a ScrollViewer which can be found by VisualTreeHelper, you can get the HorizontalOffset or VerticalOffset by ScrollViewer.HorizontalOffset property or ScrollViewer.VerticalOffset property in ScrollViewer.ViewChanged event.Then you can use the two offsets and ScrollViewer.ViewportHeight or ScrollViewer.ViewportWidth to caculate the index of the listview first visible item.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, February 27, 2014 3:06 AM

All replies

  • Hi,

    In windows store app C#, there is no direct way to get the listview first visible item when scroll. But in WinJS, it has ListView.indexOfFirstVisible property which can get or set the first visible item in Listview.

    But I think you can create a similar way to implement such behavior, ListView or GridView contains a ScrollViewer which can be found by VisualTreeHelper, you can get the HorizontalOffset or VerticalOffset by ScrollViewer.HorizontalOffset property or ScrollViewer.VerticalOffset property in ScrollViewer.ViewChanged event.Then you can use the two offsets and ScrollViewer.ViewportHeight or ScrollViewer.ViewportWidth to caculate the index of the listview first visible item.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, February 27, 2014 3:06 AM
  • Hi 

    Thanks for the great explanation. Ive got the visual tree working and Im getting the scrollview values. But Im not sure on how I can calculate the index of the first visible item?
    Sorry if Im being a bit noob :)

    Thanks 
    Saturday, March 1, 2014 1:43 AM
  • Sorry for late reply I figured it out but busy with work. Thanks for the help.
    Wednesday, March 12, 2014 1:06 AM
  • I think that the solution does not work if the items are not of the same width...
    • Edited by ivan.icin Wednesday, March 4, 2015 4:11 PM
    Wednesday, March 4, 2015 4:10 PM