Silverlight supports ListBox scroll position

I have a ListBox control that I use an ObservableCollection to add items to my ViewModel, but I noticed that the ListBox does not support the scroll position as I expected.

I am new to Silverlight and decided to go with MVVM, but I cannot figure out how to do this. The scenario is as follows:

  • Get the scroll position of a ListBox (do I need to access the xaml control, but inside the ViewModel?)
  • Add an item to the ObservableCollection (done only through Dispatcher.CheckBeginInvokeOnUI in the ViewModel, the ListBox ItemSource control is attached to it).
  • Set ListBox scroll position (do you need access to xaml control from ViewModel again?)

I found this answer here: Restoring the exact scroll position in Windows Phone 7

And with a little change, I think that the vertical scroll position can be selected and set using:

ScrollViewer sv = TimelineTweets.Descendents().OfType<ScrollViewer>().FirstOrDefault();
double startOffset = sv.VerticalOffset;
sv.ScrollToVerticalOffset(startOffset);

But how could I do this with MVVM and Silverlight in general, very confused.

+3
source share
2 answers

I would suggest that the scroll position is something for the user interface, so handle it in the interface code. This will not violate MVVM, because you are still handling all of this in your ViewModel.

+1
source

you can save it with

(DataContext as VM).ScrollVOffset=sv.VerticalOffset 

inside your view. Where VM is the ViewModel class, and ScrollVOffset is a property of the ViewModel class.

0
source

All Articles