user2350774 user2350774 -4 years ago 193
C# Question

WPF ScrollViewer - "Page Up/Down" keys goes all the way to the start/end?

In XAML, I have set up a Grid, containing a ScrollViewer, containing a TextBox:

<Window x:Class="Test.MainWindow"
Title="MainWindow" Height="350" Width="525" Loaded="MainWindow_OnLoaded">
<TextBox Name="TempTextBox" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"></TextBox>

Then I (programatically in C#) added a bunch of text to that text box. Many lines. Mostly, vertical scrolling works as I would expect: I can click and drag the scroll bar grabberthing; I can click in the scroll bar above or below the grabberthing; I can use the up and down arrow keys; I can use Ctrl-Home and Ctrl-End. All of that works fine. But:

If I use the Page Up or Page Down key, it's as if I have instead used Ctrl-Home or Ctrl-End. That is, it goes to the top or the bottom of the text, rather than up or down one page.

Am I doing something wrong? I'm pretty sure it's not an issue like "your Ctrl key is stuck down", because Page Up and Page Down work fine in other applications on my computer.

Answer Source

Remove ScrollViewer.

TextBox has scrolling support out of box, it doesn't need this addictional control 'ScrollViewer' to have scrolling. Set ScrollViewer.VerticalScrollBarVisibility="Auto" in your TextBox, but don't wrap up your TextBox with ScrollViewer and you are good to go.

You are giving two scrolling possibilities which are conflicting with each other.

I have a TextBox with ScrollViewer.VerticalScrollBarVisibility="Auto" without ScrollViewer and Page Up and Page Down works fine.

To test, I added 'ScrollViewer' around my 'TextBox', then it started to show the same problema that you have stated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download