Search code examples
c#uwpkeyboard-shortcutsacceleratorkey

Use Contrl+(Shift)+Tab to cycle through tabs/headers in Pivot Control? (UWP)


I'm trying to use KeyboardAccelerators to change pages in a PivotControl. I used the code from the documentation:

<Pivot x:Name="rootPivot" Title="PIVOT TITLE">
    <Pivot.RightHeader>
        <CommandBar ClosedDisplayMode="Compact">
            <AppBarButton Icon="Back" Label="Previous" Click="BackButton_Click"/>
            <AppBarButton Icon="Forward" Label="Next" Click="NextButton_Click"/>
        </CommandBar>
    </Pivot.RightHeader>
    <PivotItem Header="Pivot Item 1">
        <!--Pivot content goes here-->
        <TextBlock Text="Content of pivot item 1."/>
    </PivotItem>
    <PivotItem Header="Pivot Item 2">
        <!--Pivot content goes here-->
        <TextBlock Text="Content of pivot item 2."/>
    </PivotItem>
    <PivotItem Header="Pivot Item 3">
        <!--Pivot content goes here-->
        <TextBlock Text="Content of pivot item 3."/>
    </PivotItem>
</Pivot>

and code behind:

public MainPage() {
        InitializeComponent();
        KeyboardAccelerator goRight = new KeyboardAccelerator() {
            ScopeOwner = rootPivot,
            Modifiers = Windows.System.VirtualKeyModifiers.Control,
            Key = Windows.System.VirtualKey.Tab
        };
        goRight.Invoked += (s, e) => {
            e.Handled = true;
            int index = rootPivot.SelectedIndex;
            index += 1;
            index %= rootPivot.Items.Count;
            rootPivot.SelectedIndex = index < 0 ? index + rootPivot.Items.Count : index;
        };
        rootPivot.KeyboardAccelerators.Add(goRight);

        KeyboardAccelerator goLeft = new KeyboardAccelerator() {
            ScopeOwner = rootPivot,
            Modifiers = Windows.System.VirtualKeyModifiers.Control | Windows.System.VirtualKeyModifiers.Shift,
            Key = Windows.System.VirtualKey.Tab
        };
        goLeft.Invoked += (s, e) => {
            e.Handled = true;
            int index = rootPivot.SelectedIndex;
            index -= 1;
            index %= rootPivot.Items.Count;
            rootPivot.SelectedIndex = index < 0 ? index + rootPivot.Items.Count : index;
        };
        rootPivot.KeyboardAccelerators.Add(goLeft);
    }

The problem is that neither accelerator is invoked. I can see in the live property viewer that Ctrl+Tab is registered (can't find Ctrl+Shift+Tab). Is there any native behaviour that needs to be overridden? Thanks for the help.


Solution

  • The problem is that neither accelerator is invoked. I can see in the live property viewer that Ctrl+Tab is registered

    It's because the Ctrl+Tab is default keyboard behavior for common UWP controls. If you do not add any keyboard accelerators for Pivot control, then, you press Ctrl+Tab, it still will switch between PivitItems. If you change it to Ctrl+Z, the Invoked event will be fired.