I'm working on making an app come back nicely from being tombstoned. The app contains large listboxes, so I'd ideally like to scroll back to wherever the user was while they were scrolling around those listboxes.
It's easy to jump back to a particular SelectedItem - unfortunately for me, my app never needs the user to actually select an item, they're just scrolling through them. What I really want is some sort of MyListbox.ScrollPositionY but it doesn't seem to exist.
Any ideas?
Chris
You need to get hold of the
ScrollViewer
that is used by theListBox
internally so you can grab the value of theVerticalOffset
property and subsequently call theSetVerticalOffset
method.This requires that you reach down from the
ListBox
through the Visual tree that makes up its internals.I use this handy extension class which you should add to your project (I've gotta put this up on a blog because I keep repeating it):-
With this available the
ListBox
(and all other UIElements for that matter) gets a couple of new extension methodsDescendents
andAncestors
. We can combine those with Linq to search for stuff. In this case you could use:-