Can't scroll with touch in your WPF app? Make sure to set the correct PanningMode!

So you have some content in a WPF (Windows Presentation Foundation) UI that might not always fit in its parent and so you put it in a ScrollViewer. You then try to scroll it using the panning gesture on a touch screen, only to find that the content doesn't scroll. Don't worry, it's not that WPF doesn't support scrolling with the pan gesture on a touch screen - you just have to enable it!

How to enable touch panning on a ScrollViewer to scroll in WPF

To enable using the touch screen pan gesture to scroll a ScrollViewer's contents, just use the PanningMode.

Here's how to do it in XAML:

<ScrollViewer PanningMode="Both">

C#:

ScrollViewer.PanningMode = PanningMode.Both;

And VB:

ScrollViewer.PanningMode = PanningMode.Both

Make sure to replace 'ScrollViewer' in the above C# and VB code with the name of the ScrollViewer you want to affect!

The above examples enable panning in all directions when you perform the touch screen panning gesture, but there are other values you can use that might be more suited to your circumstances. See them here.

Want to customise how fast the ScrollViewer scrolls when using the touch pan gesture to scroll it? Check out PanningRatio and PanningDecleration.

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