Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing exact scroll-position in a Listview

Tags:

c#

wpf

Been searching around the net for the answer but I haven't found anything that does this:

I want to change the exact position in a WPF-list iew programmatically. Some way of saying

ListView.Scrollposition.Y = some value;

The only thing I can find is to change the value to a object within the Listview, not specific coordinates. If anyone has some good articles or posts in this subject, I'd be very grateful!

Thanks.

like image 326
Tokfrans Avatar asked Dec 16 '22 02:12

Tokfrans


2 Answers

There is no way of doing that using the ListView element. Instead, you need to access its ScrollViewer and then you can use the ScrollViewer.ScrollToVerticalOffset Method to set the vertical position of the ScrollViewer. You'll also need to use the ScrollViewer.VerticalOffset, ScrollViewer.VerticalOffset, ScrollViewer.ViewportHeight and ScrollViewer.ExtentHeight properties to gauge where you are in the ScrollViewer.

From the ScrollViewer Class page on MSDN:

The area that includes all of the content of the ScrollViewer is the extent. The visible area of the content is the viewport.

Finally, how do you get the ScrollViewer from the ListView? I can't guarantee that this will work on a ListView, but it does on a ListBox. you can use the VisualTreeHelper.GetChild Method to delve into the visual tree of the ListView and it should contain a Border and then the ScrollViewer, so you should be able to do something like this:

Border border = (Border)VisualTreeHelper.GetChild(YourListView, 0);
ScrollViewer scrollViewer = VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
if (scrollViewer != null)
{
    scrollViewer.ScrollToVerticalOffset(60.0);
}

If you do get an error with what the GetChild method returns, it'll be easy to adjust by debugging it. Just put a breakpoint there and see what each child type is and add another line with one of those elements... eventually, it should find the ScrollViewer. However, I think that that code should be fine.

like image 177
Sheridan Avatar answered Jan 26 '23 00:01

Sheridan


If it's a ListView then first you'll need to find ScrollViewer:

private ScrollViewer FindScrollViewer(DependencyObject d)
{
   if (d is ScrollViewer)
      return d as ScrollViewer;

   for (int i = 0; i < VisualTreeHelper.GetChildrenCount(d); i++)
   {
      var sw = FindScrollViewer(VisualTreeHelper.GetChild(d, i));
      if (sw != null) return sw;
   }
   return null;
}

and when you find it then you can ScrollToVerticalOffset

var sw = FindScrollViewer(listView);
if (sw != null) sw.ScrollToVerticalOffset(x);
like image 43
dkozl Avatar answered Jan 26 '23 01:01

dkozl