Make your TextBoxes easier to scroll: How to enable horizontal and vertical scrollbars in a WPF TextBox in XAML, C# and VB

WPF's (Windows Presentation Foundation's) TextBox control is one of its most fundamental and useful controls - you can use it for anything from requesting small amounts of text from the user to huge chunks of text with multiple lines. It even supports spell check as well. Now, one of the issues you might find, however, when using a TextBox for accepting large amounts of text is that it might be hard for the user to scroll through the text in a TextBox. By default, the only ways to scroll a WPF TextBox are a) to move the text cursor and the TextBox will scroll automatically to the location of the cursor, b) to scroll vertically with a scrollwheel or touchpad gesture (More info on that here) or c) to scroll with a touch screen using the panning gesture (through to scroll horizontally you must first start scrolling vertically). As you can tell, these options are limited - moving the text cursor to scroll is a slow and cumbersome process and with a scroll wheel or touchpad you can only scroll horizontally as WPF does not support horizontal touchpad or mouse wheel scrolling (More info on that here). To make this easier, you can enable vertical and/or horizontal scroll bars in your TextBox to help make it easier for users to scroll the TextBox's content.

WPF TextBox without vertical and horizontal scroll bars

WPF TextBox with vertical and horizontal scroll bars

How to enable scroll bars on a WPF TextBox

To enable scroll bars on a TextBox, you can simply use ScrollViewer.VerticalScrollBarVisibility and ScrollViewer.HorizontalScrollBarVisibility from the System.Windows.Controls namespace. You can choose from 4 scrolling behaviours for both horizontal and vertical scrolling:

  • Auto - this option shows the scroll bar only if the content is scrollable (i.e. not all the content is currently visible on screen and can be scrolled to see more content)
  • Visible - Scroll bar is always visible
  • Hidden - Scroll bar is hidden
  • Disabled - Scrolling in this direction is completely disabled
Here's an example of how to use this on a TextBox to enable scroll bars in XAML:

<TextBox ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible"/>

And in C#:

ScrollViewer.SetVerticalScrollBarVisibility(TextBox, ScrollBarVisibility.Visible);

ScrollViewer.SetHorizontalScrollBarVisibility(TextBox, ScrollBarVisibility.Visible);

And VB:

ScrollViewer.SetVerticalScrollBarVisibility(TextBox, ScrollBarVisibility.Visible)

ScrollViewer.SetHorizontalScrollBarVisibility(TextBox, ScrollBarVisibility.Visible)


Obviously, make sure to replace 'TextBox' with the name of the TextBox you wish to affect.

Bonus tip: You can use ScrollViewer.SetPanningMode to change the touch screen scrolling behaviour! View its documentation here for more information!

Extra bonus tip: You can enable a more modern and performant touch scrolling experience by setting WPF to use pointer events instead of the older WISP (Windows Ink Services Platform) which you can do by enabling the Switch.System.Windows.Input.Stylus.EnablePointerSupport app context switch. One way to do this is to use the AppContext.SetSwitch function with the following arguments: AppContext.SetSwitch("Switch.System.Windows.Input.Stylus.EnablePointerSupport", "True"). View the document for this feature here.

This snippet is available in Codly. Click the appropriate link below to download the snippet. If you don't have Codly, it is available here in the Microsoft Store.

Comments

Popular posts from this blog

How to show placeholder text in a WPF TextBox in C# and VB

How to enable dark title bar in WPF and Windows Forms/WinForms apps in C# and VB

How to use modern icons in XAML in WPF on Windows 10 and 11

How to change the colour of a WPF or Windows Forms/WinForms title bar in Windows 11 in C# and VB

Microsoft WebView2: How to check if the WebView2 runtime is installed in C# and VB