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.
Comments
Post a Comment