WPF and MVVM: saving scrolling Design and installation on reboot

I have a ScrollViewer for a StackPannel. Users want to keep the position of the ScrollViewer, so when the application is loaded with their data, the StackPannel will show the items they viewed before. This has nothing to do with which items were selected, if any, only the ScrollViewer potion regarding the StackPannel items. So, if the StackPannel has 50 elements, and the ScrollViewer scrolls so that 20-25 StackPannel elements are visible, I need to restart the application and scroll down to this position without selecting the element. In addition, I use MVVM, and I would also like to set the position of the ScrollViewer using the ViewModel code.

+4
source share
3 answers

Below the sample will save the scroll offset in the virtual machine and load it when the window (TestWindow) opens. You should also store and load the window size, as this will most likely affect the scroll shift. If you want, you can move the code in TestWindow to an attached behavior class.

XAML:

<Window x:Class="ScrollTest.TestWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestWindow" Height="200" Width="300" Loaded="OnLoaded" Closing="OnClosing"> <Grid> <ScrollViewer Name="_scroll" VerticalScrollBarVisibility="Auto"> <StackPanel> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> <Button Content="Click me" /> </StackPanel> </ScrollViewer> </Grid> </Window> 

Code behind:

 using System; using System.ComponentModel; using System.Windows; namespace ScrollTest { public partial class TestWindow : Window { public TestWindow() { InitializeComponent(); } private void OnLoaded(object sender, RoutedEventArgs e) { _scroll.ScrollToVerticalOffset((DataContext as VM).ScrollOffset); } private void OnClosing(object sender, CancelEventArgs e) { (DataContext as VM).ScrollOffset = _scroll.VerticalOffset; } } public class VM { public double ScrollOffset { get; set; } } } 

Using:

 private void OnOpenOpenTestWindow(object sender, RoutedEventArgs e) { TestWindow testWindow = new TestWindow(); testWindow.DataContext = _vm; testWindow.Show(); } private VM _vm = new VM(); 
+5
source

You can also set the position of the ScrollViewer by calling the ScrollToVerticalOffset Method.

 contentScrollViewer.ScrollToVerticalOffset(50); 
0
source

Have you studied using the ScrollViewer.ScrollInfo property? It has elements that represent the offset of each scrollbar ( HorizontalOffset , VerticalOffset ) with which you could try to snap. However, I'm not sure if these are dependency properties.

Another option would be to find the actual ScrollBar controls in the visual tree and bind to each of their Position property.

-1
source

All Articles