ScrollViewer

The ScrollViewer control allows oversized content to be displayed by putting it into a scrollable area. A ScrollViewer element has a single child. Example 3-35 uses an Ellipse element, but it could be anything. If you want to put multiple elements into a scrollable view, you would nest them inside a panel.

Example 3-35. ScrollViewer

<ScrollViewer HorizontalScrollBarVisibility="Auto">
  <Ellipse Fill="Green" Height="1000" Width="2000" />
</ScrollViewer>

If the content of a ScrollViewer is larger than the space available, the ScrollViewer can provide scroll bars to allow the user to scroll around the content, as ScrollViewer shows. By default, a ScrollViewer provides a vertical scroll bar, but not a horizontal one. In Figure 3-39, the HorizontalScrollBarVisibility property has been set to Auto, indicating that a horizontal scroll bar should be added if required.

ScrollViewer

Figure 3-39. ScrollViewer

This Auto visibility we've chosen for the horizontal scroll bar is different from the default vertical behavior. The VerticalScrollBarVisibility defaults to Visible, meaning that the scroll bar is present whether it is required or not.

There are two ways to make sure a scroll bar does not appear. You can set its visibility either to Disabled (the default for horizontal scroll bars) or to Hidden. The distinction is that Disabled constrains the logical size of the ScrollViewer's contents to be the ...

Get Programming WPF, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.