Using XAML ScrollViewer in WPF

The XAML ScrollViewer element adds scrolling functionality to an area that may host other controls. This tutorial shows you how to use a ScrollViewer control in WPF.

Introduction

The ScrollViewer element in XAML represents a WPF ScrollViewer control.

  1. <ScrollViewer></ScrollViewer>  
The HorizontalScrollBarVisibility and VerticalScrollBarVisibility are two major used properties of ScrollViewer that enables horizontal and vertical scrolling. These properties are represented by a ScrollBarVisibility enumeration that has the four values Auto, Disabled, Hidden and Visible.

The Disabled and Hidden values are used to disable and hide scrolling on WPF controls. Visible is used to make it visible all the time. Auto is the most used value that makes scrolling visible when it is needed only.

The following adds scrolling functionality to a TextBlock.
  1. <Window x:Class="ScrollViewerSample.Window1"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.     Title="Window1" Height="300" Width="300">  
  5.       
  6.     <ScrollViewer HorizontalScrollBarVisibility="Auto">        
  7.         <TextBlock TextWrapping="Wrap" Width="260" Height="400" VerticalAlignment="Top"  >  
  8.             This TextBlock is placed within a ScrollViewer control  
  9.             with automatic scrolling enabled. If text goes outside of the width   
  10.             or height, scrolling will be enabled automatically.  
  11.             This TextBlock is placed within a ScrollViewer control  
  12.             with automatic scrolling enabled. If text goes outside of the width   
  13.             or height, scrolling will be enabled automatically.  
  14.         </TextBlock>  
  15.     </ScrollViewer>  
  16.   
  17. </Window>  
The ScrollViewer looks like Figure 1.

ScrollViewer
                                 Figure 1

Up Next
    Ebook Download
    View all
    Learn
    View all