Implementing Scrolling in Windows using WPF ScrollViewer in C# and XAML

Content and data focused Windows applications often require vertical and/or horizontal scrolling functionality to conveniently display large content on the screen. The WPF ScrollViewer control can be used to implement vertical and horizontal scrolling on Windows controls.
 
 
To implement scrolling functionality on a content control, the content control is placed as the default content of a ScrollViewer control using its Content property. A ScrollViewer can only have one child, typically a Panel element that can host a Children collection of elements.
 
The simplest way to enable scrolling on a content control is by placing the content control inside a ScrollViewer control. The following code snippet places a StackPanel control within a ScorllViewer control. 
  1. <ScrollViewer HorizontalScrollBarVisibility="Auto">  
  2. <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">  
  3. <TextBlock>Resize window to auto enable or disable scrolling.</TextBlock>  
  4. <Separator />  
  5. <Rectangle Fill="DodgerBlue" Width="500" Height="500"></Rectangle>  
  6. </StackPanel>  
  7. </ScrollViewer>  
The code places a TextBlock and a Rectangle control on a StackPanel. The StackPanel is placed within a ScrollViewer. The scrolling is enabled by default and both horizontal and vertical scrollbars are visible because the content (Rectangle) is larger than the parent control, StackPanel.
 
You can resize the Window and you will see the ScrollViewer appears and disappears when the Windows is smaller and larger than the content, respectively.
 
The ScrollViewer control responds to both mouse and keyboard commands and defines numerous methods with which to scroll content by predetermined increments. You can use the ScrollChanged event to detect a change in a ScrollViewer state.
 
The VerticalScrollBarVisibility and HorizontalScrollBarVisibility properties enable or disable vertical and horizontal scrolling capabilities on a ScrollViewer. It has four values - Auto, Disabled, Hidden, and Visible that are self-explanatory. In most cases, you may want to keep the default Auto property that shows or hides scroll bars as needed.
 
The following code snippet sets both, vertical and horizontal scrollbars to Auto. 
  1. <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Visible" >  
In our case, both scrollbars are visible.
 
WPF ScrollViewer Enable 
 
You can disable and hide scrollbars by setting these values to Disabled and Hidden respectively. The Visible value makes sure the scrollbars are visible all the time regardless of the content needed scrolling or not.
 
Let’s add a ScrollViewer control as the root content of a Window. Then place a TextBlock control and few more child controls as a part of the content. 
  1. <Window x:Class="WPFScrollViewerSample.MainWindow"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6. xmlns:local="clr-namespace:WPFScrollViewerSample"  
  7. mc:Ignorable="d"  
  8. Title="MainWindow" Height="400" Width="400">  
  9.   
  10. <ScrollViewer VerticalScrollBarVisibility="Auto">  
  11. <TextBlock TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" Padding="20" >  
  12. <InlineUIContainer>  
  13. <TextBlock FontSize="28" FontWeight="Bold"><Run Text="Mahesh Chand"/></TextBlock>  
  14. </InlineUIContainer><LineBreak/><Run Text="Microsoft Regional Director, Founder C"/>  
  15. <Run Text="# Corner, "/><Run Text="CEO Mindcracker Inc."/><LineBreak /><LineBreak />  
  16. <Run Text="At Mindcracker Inc., I help technical teams to build better software solutions " />  
  17. <Run Text="by providing advice on architecture, design, usability, innovation, and team mentoring. " ></Run>  
  18. <Run Text="My expertise and interests include C#/.NET, UWP, Mixed-Reality, AI/ML, Cloud, Startups," ></Run>  
  19. <Run Text="and Innovation. As a founder "></Run>  
  20. <Run Text="of C# Corner, I enjoy working on new features "></Run>  
  21. <Run Text="and ideas to help developers write better code and become better human being. "></Run>  
  22. <Run Text="I love Philosophy, Cognitive Studies, Community, Public Speaking, Writing, Basketball, "/>  
  23. <Run Text="and Philadelphia Eagles."/>  
  24. </TextBlock>  
  25. </ScrollViewer>  
  26. </Window>  
The above code generates the following app that allows users to scroll its content.
 
WPF Scroll Viewer 

How to enable scrollbar in a WPF TextBox

 
The simplest way to add scrolling functionality to a TextBox control is by enabling its horizontal and vertical scrolling. The HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties are used to set horizontal and vertical scroll bars of a TextBox.
The following code snippet enables horizontal and vertical scrollbars in a TextBox control. 
  1. <TextBox x:Name="TextBox" Height="39"  
  2. HorizontalScrollBarVisibility="Visible"  
  3. VerticalScrollBarVisibility="Visible"  
  4. TextWrapping="Wrap" Margin="200,10,450,0" />  
  5. How to Add a vertical scrollbar to a WPF Grid  
  6. The following code snippet adds scrolling capability to a Grid control.  
  7. <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">  
  8. <Grid Background="AliceBlue" Width="300" Height="200"></Grid>  
  9. </ScrollViewer>  

How to Enable ScrollBar on a WPF Window

 
You can use a ScrollViewer control to enable scrollbar on a Window. The ScrollViewer needs to apply to the root container control.
The following code snippet adds scrolling to a StackPanel. 
  1. <Window x:Class="WPFScrollViewerSample.MainWindow"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6. xmlns:local="clr-namespace:WPFScrollViewerSample"  
  7. mc:Ignorable="d"  
  8. Title="MainWindow" Height="400" Width="400">  
  9. <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" >  
  10. <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">  
  11. <TextBlock>Resize window to auto enable or disable scrolling.</TextBlock>  
  12. <Separator />  
  13. <Rectangle Fill="DodgerBlue" Width="450" Height="450"></Rectangle>  
  14. </StackPanel>  
  15. </ScrollViewer>  
  16. </Window>  


Recommended Free Ebook
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.