2012-12-04 4 views
0

WPF 창에 단추가 있습니다. 왼쪽 마우스 클릭을 누르고 Ctrl + F를 눌러 메시지를 표시하고 싶습니다. 나는 XAML에서 대부분의 코드를 원한다. 코드는 다음과 같습니다. 내 문제는 마우스 클릭이 나를 위해 작동하지만 키 누르기가 아니라는 것입니다. 아무도 나를 도와주세요. 미리 감사드립니다.WPF CommandBinding이 작동하지 않습니다.

MyWindow.xaml 

<Window x:Class="Commands_Xaml.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="164,88,0,0" Name="button1" VerticalAlignment="Top" Width="75"> 
      <Button.CommandBindings> 
       <CommandBinding Command="ApplicationCommands.Find" Executed="CommandBinding_Executed"/> 
      </Button.CommandBindings> 
      <Button.InputBindings> 
       <KeyBinding Key="F" Modifiers="Control" Command="ApplicationCommands.Find"/> 
       <MouseBinding MouseAction="LeftClick" Command="ApplicationCommands.Find"/> 
      </Button.InputBindings> 
     </Button> 
    </Grid> 
</Window> 


MainWindow.xaml.cs 

namespace Commands_Xaml 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent();    
      //ApplicationCommands.Find.InputGestures.Add(new KeyGesture(Key.F,ModifierKeys.Control)); 
      //ApplicationCommands.Find.InputGestures.Add(new MouseGesture(MouseAction.LeftClick)); 

      //CommandBinding bindingObject = new CommandBinding(); 
      //bindingObject.Command = ApplicationCommands.Find; 
      //bindingObject.Executed += new ExecutedRoutedEventHandler(CommandBinding_Executed); 
      //this.CommandBindings.Add(bindingObject); 
     } 

     private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) 
     { 
      MessageBox.Show("Button clicked"); 
     } 
    } 
} 
+0

명령에 올바른 마우스 명령 이벤트 방법을 사용하고 있습니까? –

+0

예 Bob, 목적을 이해하기 위해 코드를 작성했습니다. 마우스 클릭에는 작동하지만 키 입력에는 작동하지 않습니다. – WpfBee

답변

2

명령 바인딩/입력 바인딩을 창 수준으로 이동하고 MouseBinding을 사용하는 대신 단추의 Command 속성을 사용하십시오.

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.CommandBindings> 
     <CommandBinding Command="ApplicationCommands.Find" Executed="CommandBinding_Executed" /> 
    </Window.CommandBindings> 
    <Window.InputBindings> 
     <KeyBinding Command="ApplicationCommands.Find" Key="F" Modifiers="Control" /> 
    </Window.InputBindings> 

    <Grid> 
     <Button Command="ApplicationCommands.Find" Content="Button" HorizontalAlignment="Left" Margin="206,152,0,0" VerticalAlignment="Top" Width="75"/> 
    </Grid> 
</Window> 

UPDATE

다른 버튼 클릭에 따라 다른 동작을하려면이 몇 가지 방법을 수행 할 수 있습니다.

<Button Command="ApplicationCommands.Find" Content="Button1" /> 
<Button Command="ApplicationCommands.Print" Content="Button2" /> 

사용 CommandParameter :

각 버튼에 대해 다른 명령을 사용하여 다음 두 가지가 있습니다

<Button Command="ApplicationCommands.Find" CommandParameter="Find1" Content="Button1" /> 
<Button Command="ApplicationCommands.Find" CommandParameter="Find2" Content="Button2" /> 

그리고 키 바인딩을 위해 :

<KeyBinding Command="ApplicationCommands.Find" CommandParameter="Find3" /> 

는이 속성에 액세스 할 수 있습니다 CommandBinding.Executed 처리기에서 :

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) 
{ 
    if (e.Parameter as string == "Find1") 
    { 
     //Find1 
    } 
    else if (e.Parameter as string == "Find2") 
    { 
     //Find2 
    }  
    else if (e.Parameter as string == "Find3") 
    { 
     //Find3 
    } 
} 
+0

RQDQ에 감사드립니다. 귀하의 제안은 저에게 도움이되었습니다.하지만 몇 가지 질문이 있습니다 : 1. 마우스를 클릭하면 MouseBinding을 할 필요가 없습니다. 그렇다면 왜 그럴까요? 마우스 클릭은 특정 작업을 수행하지 않고도 작동하기 때문에? 2. 하나만 더 버튼을 갖고 싶습니다.이 2 개의 버튼에 공통된 이벤트 핸들러가 필요합니다.이 이벤트 핸들러 내에서 다른 버튼 클릭에 따라 일부 작업을 수행하려고합니다. 어떻게해야합니까?이 질문을하는 이유는 무엇입니까? CommandBinding_Executed()에서 보낸 사람을 보았을 때 창으로 발견했으며 창 정보 대신 버튼 정보를 얻고 싶습니다. – WpfBee

+0

@WpfBee - 다른 명령 호출을 기반으로 다른 동작을 수행하는 몇 가지 방법이 있습니다. 나는 둘을 더했다. – RQDQ

+0

@ RQDQ- 내 쿼리 # 1, 대답 해 주시겠습니까? 쿼리 # 2, 내가 받고 있지 않은 명령 처리기 안에 보낸 사람으로 단추 개체를 받기를 원합니다. 수신 명령 매개 변수 (Find1/Find2)는 마우스 클릭을 사용할 때 작동하지만 Qtrl + F를 시도하면 명령 핸들러에서 수신 된 명령 매개 변수가 null입니다. – WpfBee