Make scrollviewer not grab control on the left and control in WPF

I have a WPF application where I defined user commands that use the Ctrl+ and Ctrl+ key gestures . In my window there TextBoxand a ScrollViewer, and when the TextBoxfocus is on Ctrl+ and Ctrl+ to move the cursor to the beginning of the next or previous word, which is good.

But on ScrollViewerit moves the horizontal scroll as if I pressed only or . What is the easiest way to allow events Ctrl+ and Ctrl+ to go from mine ScrollViewerto the window where it is defined Command?

+5
source share
2 answers

So, I got this work by doing the following

public class MyScrollViewer : ScrollViewer
{
    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyboardDevice.Modifiers == ModifierKeys.Control)
        {
            if (e.Key == Key.Left || e.Key == Key.Right)
                return;
        }
        base.OnKeyDown(e);
    }
}

I think the trick is to understand that the event starts from the top and works down. Then, along the backup path, each “layer” checks the processed logical value, and if it is not set to true, it will determine whether it will do something and set it if that happens. I needed to short-circuit this behavior for my shortcuts so that Handled would still be false when it returned to the Window level.

+4
source

ScrollViewer PreviewKeyDown e.Handled = true , - (this.Parent as FrameworkElement).RaiseEvent(e);, , .

+3

All Articles