0

I have a list of items and beside it I have buttons. I want functionality in such a way that, when I click on button, the corresponding item from the list view has to display on the top of the list. For this I thought scrollintoview will work, but unfortunately it is not working. Any suggestions?

  ....
  lstview.SelectedItem = lstview.Items.GetItemAt(lstview.SelectedIndex);
  lstview.ScrollIntoView(lstview.SelectedItem);
  lstview.UpdateLayout();
  .....
sainath sagar
  • 499
  • 2
  • 10
  • 28

1 Answers1

0

ScrollIntoView method will just bring the item into view, if it is not visible. If it is already visible, then calling the method will not cause any effect. In your case, if you are able to click the item, obviously the item is in the view and there is no need to scroll. The explanation from MSDN.

If the object is not visible, it is aligned at the top or bottom of the viewport.

If you always want that item to be the first item in viewport, then you need to adjust the VerticalOffset. Get the Y value of your ListBox and scroll the scrollviewer to Y value.

listBoxScrollViewer.ScrollToVerticalOffset(100); // Measure the Y value of your listbox.

You can get the scrollviewer from ListBox like this.

Community
  • 1
  • 1
Jawahar
  • 4,775
  • 1
  • 24
  • 47