Delikatessen Berglunds Delikatessen Berglunds - 29 days ago 17
C# Question

How to navigate between button(focus) when press Tab in wpf?

I have set it for F1, F3, F7 for three buttons. The problem is, the button must be focused before pressing F1 or F3 ...
I want to use TAB on keyboard to focus between buttons.



<Grid Grid.Row="0" Background="#373737">
<StackPanel Orientation="Horizontal" Margin="0,0,0,1">
<Button x:Name="btnPay" FontSize="13" FontWeight="ExtraBold" Margin="0,0,1,0" BorderThickness="0" Width="120" KeyDown="btnPay_KeyDown">
<StackPanel Orientation="Vertical">
<Image Source="/Image/receipt.png" Width="40" Height="40" />
<TextBlock VerticalAlignment="Center" Margin="5,0" Foreground="White">Thanh toán (F1)</TextBlock>
</StackPanel>
</Button>
<Button x:Name="btnClear" FontSize="13" FontWeight="ExtraBold" Margin="0,0,1,0" BorderThickness="0" Width="120" KeyDown="btnClear_KeyDown">
<StackPanel Orientation="Vertical">
<Image Source="/Image/eraser.png" Width="40" Height="40" />
<TextBlock VerticalAlignment="Center" Margin="5,0" Foreground="White">Xóa (F2)</TextBlock>
</StackPanel>
</Button>
</StackPanel>
<Button x:Name="btnLogOut" FontSize="13" FontWeight="ExtraBold" Width="120" Margin="0,0,1,1" BorderThickness="0" KeyDown="btnLogOut_KeyDown"
HorizontalAlignment="Right">
<StackPanel Orientation="Vertical">
<Image Source="/Image/password.png" Width="40" Height="40" />
<TextBlock VerticalAlignment="Center" Margin="5,0" Foreground="White">Log Out (F6)</TextBlock>
</StackPanel>
</Button>
</Grid>

Answer

Your workaround seems to be an additional problem instead of a solution. Instead of using the PreviewKeyDown events of all the buttons, simply use the one of your Window and decide what to do based on the key pressed.

Window:

<Window PreviewKeyDown="Window_PreviewKeyDown">

Code behind:

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
    bool handled = true;
    if (!Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
    {
        switch (e.Key)
        {
            case Key.F1:
                {
                    Play();
                    break;
                }
            // ...
            case Key.F6
                {
                    LogOut();
                    break;
                }
            default:
                handled = false;
                break;
        }
    }
    else
    {
        switch (e.Key)
        {
            case Key.C:
                {
                    Copy();
                    break;
                }
            case Key.V:
                {
                    Paste();
                    break;
                }
            case Key.Z:
                {
                    Undo();
                    break;
                }
            case Key.Y:
                {
                    Redo();
                    break;
                }
            default:
                handled = false;
                break;
        }
    }
    e.Handled = handled;
}

Alternatively (this would be en even better solution) you could implement your commands as ICommand and use them for input bindings.

<Window.InputBindings>
    <KeyBinding Modifiers="Control" Key="S" Command="{StaticResource Save}"/>
    <KeyBinding Modifiers="Control+Shift" Key="S" Command="{StaticResource SaveAs}"/>
    <KeyBinding Modifiers="Control" Key="Z" Command="{StaticResource Undo}"/>
    <KeyBinding Modifiers="Control" Key="Y" Command="{StaticResource Redo}"/>
    <KeyBinding Key="F1" Command="{StaticResource Play}"/>
    <KeyBinding Key="F6" Command="{StaticResource LogOut}"/>
</Window.InputBindings>