2009-09-10 4 views
0

몇 가지 명령 만 사용하여 WPF 응용 프로그램을 작성하므로 메뉴없이 도구 모음을 사용하고 있습니다. 도구 모음의 단추에 Ctrl 키 단축키를 지정하고 싶습니다. 바로 가기를 지원하기 위해 라우팅 된 명령이나 ICommand를 만들지 않고도이 작업을 수행 할 수있는 간단한 방법이 있습니까? 코드 숨김 대신 XAML에서 수행하는 것을 선호합니다.도구 모음의 키보드 단축키

답변

0

WPF가 Ctrl 키를 단추에 추가하는 간단한 방법을 제공하지 않는다는 결론에 도달했습니다. 코드 숨김에서 키 누르기를 트래핑 한 다음 적절한 ICommand (해당 단추의 Command 속성이 바인딩 된 ICommand)를 호출하여 문제를 해결했습니다. 그것은 약간 못생긴 해킹이지만 작동하지만, 상황에 따라 최선의 접근 방법 인 것 같습니다.

+2

내 대답에 대한 귀하의 의견에 따라 귀하의 질문을 이해할 수 있는지 잘 모르겠습니다. 하지만 네가 그걸 과대 평가 한 것 같아.WPF CommandBindings는 사용하는 컨트롤에 Command 특성이있는 한 필요한만큼 정확하게 수행합니다. ApplicationCommand를 설정하기 만하면 키보드 동작이 자동으로 설정됩니다. 내 예제에서 Ctrl + O를하면 CommandBinding_Executed가 트리거됩니다. – Carlo

1

코드가 필요합니다. 한 이벤트는 명령을 실행할 수 있는지 알려주고 (일반적으로 몇 줄을 넘지 않음), 다른 명령은 명령을 알려주므로 모든 컨트롤이 동일한 작업을 수행합니다. 다음은 아주 간단한 예는 다음과 같습니다

XAML :

<UserControl x:Class="WPFTests.AppCommands" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="300" Width="300"> 
    <UserControl.CommandBindings> 
     <CommandBinding Command="ApplicationCommands.New" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" /> 
    </UserControl.CommandBindings> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <CheckBox Grid.Row="0" Height="16" Name="checkBoxCanExecute" Margin="8" IsChecked="true">Can execute</CheckBox> 
     <Button Grid.Row="1" Height="24" Padding="8,0,8,0" HorizontalAlignment="Left" Margin="8" Command="ApplicationCommands.New">ApplicationCommand.New</Button> 
    </Grid> 
</UserControl> 

C#을 System.Windows를 사용

; using System.Windows.Controls; using System.Windows.Input;

namespace WPFTests 
{ 
    /// <summary> 
    /// Interaction logic for AppCommands.xaml 
    /// </summary> 
    public partial class AppCommands : UserControl 
    { 
     public AppCommands() 
     { 
      InitializeComponent(); 
     } 

     private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
     { 
      e.CanExecute = (bool)checkBoxCanExecute.IsChecked; 
     } 

     private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) 
     { 
      MessageBox.Show("New executed"); 
     } 
    } 
} 

편집 : RichardOD의 요청에 따라 편집하십시오. 여기에 내가 내 댓글에 게시 조금 더 많은 정보는 다음과 같습니다

<Button Command="ApplicationCommands.New"> 

이 버튼에 키보드 제스처 Ctrl 키 + N을 둔다. 당신이

<Button Command="ApplicationCommands.Open"> 

작업을 수행하는 경우 그것은이 암시 WPF의있는 CommandBindings에서 구현 Ctrl 키 + O.을 둘 것입니다. 자신 만의 제스처로 자신 만의 명령을 만들 수도 있습니다. 이 메뉴 항목에 명령을 결합하는 경우에, 자동 단지, 여분의 efforr하지 않고 명령 이름 옆에있는 제스처를 보여줍니다 :

<MenuItem Command="ApplicationCommands.New" /> 

이 메뉴 항목에서 [새 Ctrl 키 + N]를 보여줄 것입니다.

+0

응답 해 주셔서 감사합니다.하지만 답변을 드리지는 못했습니다. 버튼에 Ctrl 키를 입력하면됩니다. 예를 들어 Ctrl-O를 눌러 '파일 열기'버튼을 호출하십시오. –

+1

+0

+1 카를로, 귀하의 의견은 답변이기 때문에 귀하의 의견을 답변으로 옮길 것입니다. :) – RichardOD

0

가장 간단하고 더러운 방법은 직접 키보드 단축키를 추가하는 것입니다. 물론 Command에서 사용하는 단축키를 변경하는 경우 도구 설명에서 수동으로 변경해야합니다. 하지만 도구 팁 텍스트를 Command의 속성에 바인딩하여 바인딩을 통해 바로 가기를 가져올 수 있습니까? 가능하면 누구나 아는가?

 <Button Command="{Binding OpenDllCommand}" Content="Bla-bla > 
      <Button.ToolTip> 
       <StackPanel> 
        <TextBlock Text="F6" FontWeight="Bold" /> 
        <TextBlock Text="Open MbUnit/MINT DLL with tests" /> 
       </StackPanel> 
      </Button.ToolTip> 
     </Button> 
관련 문제