Creating a scrollable user interface
There are occasions when some data to display is larger than the display area; this requires scrolling capabilities. WPF provides that using a simple control, the ScrollViewer
.
Getting ready
Make sure Visual Studio is up and running.
How to do it...
We'll create a simple image viewer that provides scrollbars if necessary when viewing a large image:
Create a new WPF application named
CH03.ScrollDemo
.Add an image to the project. For example, you could select the
Penguins.jpg
file from the Pictures | Sample Pictures folder.Open
MainWindow.xaml
. Add aScrollViewer
control inside the existingGrid
.Inside the
ScrollViewer
, add anImage
element, and set itsSource
property to the image you added:<ScrollViewer> <Image Source="penguins.jpg" Stretch="None" /> </ScrollViewer>
Set the
Image Stretch
property toNone
. This ensures the image is displayed in its original size.Run the application. You should be able to scroll vertically as needed,...