#889 – Scrolling a ScrollViewer from Code
August 21, 2013 2 Comments
You can programmatically cause a ScrollViewer’s content to scroll by using one or more of the ScrollViewer’s methods listed below.
To scroll content vertically:
- Call LineUp to scroll up one line
- Call LineDown to scroll down one line
- Call PageUp to scroll up one page
- Call PageDown to scroll down one page
- Call ScrollToHome to scroll to the top
- Call ScrollToEnd to scroll to the bottom
To scroll horizontally, use the corresponding methods LineLeft, LineRight, PageLeft, PageRight, ScrollToLeftEnd and ScrollToRightEnd.
<StackPanel Orientation="Horizontal"> <Button Content="Up 1" Click="btnUpOne_Click"/> <Button Content="Down 1" Click="btnDownOne_Click"/> </StackPanel> <ScrollViewer Grid.Row="1" Name="svMain" VerticalScrollBarVisibility="Visible"> <StackPanel> <Image Source="Augustus.jpg" Height="100" Margin="5"/> <Image Source="Tiberius.jpg" Height="100" Margin="5"/> <Image Source="Caligula.jpeg" Height="100" Margin="5"/> <Image Source="Claudius.jpg" Height="100" Margin="5"/> <Image Source="Nero.jpg" Height="100" Margin="5"/> <Image Source="Galba.jpg" Height="100" Margin="5"/> </StackPanel> </ScrollViewer>
private void btnUpOne_Click(object sender, RoutedEventArgs e) { svMain.LineUp(); } private void btnDownOne_Click(object sender, RoutedEventArgs e) { svMain.LineDown(); }
Pingback: Dew Drop – August 21, 2013 (#1,608) | Alvin Ashcraft's Morning Dew
Pingback: #931 – Scrolling Text in a TextBox from Code | 2,000 Things You Should Know About WPF