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.
source
share