2017-12-23 28 views
0

버튼 클릭 이벤트를 활성화하려면 :간단한 키보드 단축키 내가 버튼이 매우 간단한 WPF 응용 프로그램이

<Button x:Name="buttonNextImage" Content="&gt;" Margin="0,0,0.4,-0.2" 
     Click="buttonNextImage_Click" HorizontalAlignment="Right" Width="25" 
     Background="Transparent" 
     BorderThickness="0"> 
</Button> 

내가 원하는 모든 사용자가 오른쪽 화살표를 눌러이 버튼의 클릭 이벤트를 활성화 할 수있다 응용 프로그램 창에 포커스가있을 때 keybaord 키를 누릅니다.

this answer을 검사했지만 밑줄을 추가 할 레이블이 없습니다. 버튼을 표시하려는 유일한 텍스트는 >입니다. 라벨을 추가하지 않고도이 작업을 수행 할 수 있습니다.

here도 대답했지만 내 xaml에서이 성가신 오류가 발생합니다. MyCommand은 코드 숨김으로 처리 했음에도 불구하고 존재하지 않습니다.

이에 대한 모든 지침은 많이 감사합니다

..

+1

왜 그냥'Window' '을 처리하지 않는다 s [KeyDown] (https://docs.microsoft.com/ko-KR/dotnet/api/system.windows.uielement.keydown) 이벤트? – JohnyL

+0

@JohnyL 유망 해 보이는 팁을 보내 주셔서 감사합니다. 결과가 업데이트됩니다. – Bassie

답변

1

당신은 <CommandBindings><InputBindings>을 구현할 수 있습니다

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication1" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.InputBindings> 
     <KeyBinding Key="Right" 
        Command="{Binding MessageCommand}" 
        CommandParameter="You pressed 'Right Arrow'"/> 
    </Window.InputBindings> 
    <Grid> 
     <Button Margin="45" Command="{Binding MessageCommand}">Click Me</Button> 

    </Grid> 
</Window> 

코드 뒤에 : 여기

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = new MyDataContext(); 
     } 

     public class MessageCommand : ICommand  
     { 
      public void Execute(object parameter) 
      { 
       string msg; 

       if (parameter == null) 
        msg = "Button Clicked!"; 
       else 
        msg = parameter.ToString(); 

       MessageBox.Show(msg); 
      } 

      public bool CanExecute(object parameter) 
      { 
       return true; 
      } 

      public event EventHandler CanExecuteChanged; 
     } 

     public class MyDataContext 
     { 
      ICommand _messageCommand = new MessageCommand(); 

      public ICommand MessageCommand 
      { 
       get { return _messageCommand; } 
      } 
     } 
    } 
} 

이 키의 목록입니다 https://msdn.microsoft.com/en-us/library/system.windows.input.key(v=vs.100).aspx

+0

이것은 전체 작동 샘플입니다. – wp78de

0

나는 JohnyL 's와 함께 갔다. 이 같은 의견에 솔루션 :

<Window KeyDown="OnKeyDownHandler"> 

코드

private void OnKeyDownHandler(object sender, KeyEventArgs e) 
{ 
    switch (e.Key) 
    { 
     case Key.Right: 
      NextImage(); 
      break; 
     case Key.Left: 
      PreviousImage(); 
      break; 
     default: 
      break; 
    } 
} 

니스와 간단한

뒤에하지만 WPF 단지 해당 컨트롤에 내장이 일을하는 쉬운 방법이없는 수치이다.

1

KeyDown 이벤트 (이전 제안 사항) 대신, ComponentCommands.MoveRight 준비 명령을 사용할 수 있습니다. > 키 또는 버튼을 누를 수 있습니다. 여기에 사용 (:있는 InputBindings 심지어 필요가 없습니다! 주)의

XAML은

<Window x:Class="WPFApp.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="450" Width="800"> 
    <Window.CommandBindings> 
     <CommandBinding Command="ComponentCommands.MoveRight" Executed="OnMoveRight"/> 
    </Window.CommandBindings> 
    <StackPanel> 
     <Button Content=">" Command="ComponentCommands.MoveRight"/> 
    </StackPanel> 
</Window> 

C#

private void OnMoveRight(object sender, ExecutedRoutedEventArgs e) 
{ 
    // Do something with image 
    MessageBox.Show("Either button or '>' key was pressed"); 
}