WPF Scrolling
WPF Scrolling
In WPF you get scrolling support by wrapping the content control you want to add scrolling to with the ScrollViewer content control.
<ScrollViewer>
<TextBlock LineHeight="20" Grid.Row="0">
</TextBlock>
</ScrollViewer>
It adds a disabled scroll which changes to enabled if the content has overflowed the size of it’s container.
You may use the VerticalScrollBarVisibility="Auto" property of the ScrollViewer if you wish the scrollbar to appear only when enabled (it’s default state is Visible) and use HorizontalScrollBarVisibility="Visible" when the horizontal scrollbar is what you need.
ScrollViewer class holds several methods that allow you to programmatically scroll the content.
- LineUp() / LineDown() / LineLeft() / LineRight() - Moves the scrollbar once up/down/left/right.
- PageUp() / PageDown() / PageLeft() / PageRight() - Moves the scrollbar a full page up/down/left/right.
- ScrollToHome – scrolls to the top of the scrollable container.
- ScrollToEnd – scrolls to the bottom of the scrollable container.
- ScrollToVerticalOffset / ScrollToHorizontalOffset – scrolls to a specific position.
For example, scrolling to a specific position:
double _pos;
double.TryParse(PositionNum.Text,out _pos);
DemoScroller.ScrollToVerticalOffset(_pos);
You may download the demo code here.
Read Pro WPF in C# 2008 by Matthew Macdonald for more WPF stuff.