2016-11-09 54 views
0

我已将它设置为F1,F3,F7三个按钮。问题是,在按下按钮之前必须集中按钮F1F3 ... 我想在键盘上使用TAB以在按钮之间进行对焦。如何在wpf中按Tab键时在按钮(焦点)之间导航?

<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> 
+0

默认情况下,“选项卡”将焦点元素更改为下一个。你可以在这里用按钮发布XAML吗?按钮上的相同布局有没有其他控件? –

+0

已上传的xaml ... –

+0

您的意思是,按F1会对'播放'执行操作,按下F2,对'Xóa'执行操作,然后按下F6对'注销'执行操作?是这样吗? –

回答

0

你的解决办法似乎是另外一个问题,而不是解决方案。 而不是使用所有按钮的PreviewKeyDown事件,只需使用您的窗口中的一个,并根据所按键决定如何操作。

窗口:

<Window PreviewKeyDown="Window_PreviewKeyDown"> 

后面的代码:

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; 
} 

或者(这将是连接更好的解决方案),你可以实现你的命令,ICommand并将其用于输入绑定。

<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> 
相关问题