2016-11-09 5 views
0

私はそれを3つのボタン用にF1,F3,F7に設定しました。問題は、ボタンは、私はボタンの間集中するキーボードのTABを使用したい ... F1またはF3を押す前に集中する必要があります。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

デフォルトでは、「Tab」はフォーカスされた要素を次の要素に変更します。ここにボタン付きのXAMLを投稿できますか?あなたは、ボタンを使って同じレイアウト上に他のコントロールを持っていますか? –

+0

アップロードされたxaml ... –

+0

F1を押すと「再生」、F2を押すと「Xóa」のアクションが実行され、F6を押すと「ログアウト」のアクションが実行されます。その場合ですか? –

答えて

0

あなたの問題を回避するには、追加的な問題はなく、解決策のようです。 すべてのボタンのイベントを使用する代わりに、ウィンドウの1つを使用して、押されたキーに基づいて何をすべきかを決定するだけです。

ウィンドウ:背後

<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> 
関連する問題