2014-10-13 3 views
1

는 나는이 같은 숫자 키패드 키에 바인딩 명령이있는 창이 있습니다WPF 무시/자식 컨트롤에 키 바인딩을 다시

 ICommand _onReasonShortcutKeyPressedCommand; 
     public ICommand OnReasonShortcutKeyPressedCommand 
     { 
      get 
      { 
       return _onReasonShortcutKeyPressedCommand ?? 
        (_onReasonShortcutKeyPressedCommand = new RelayCommand(OnReasonShortcutKeyPressedCommand_Execute)); 
      } 
     } 
     private void OnReasonShortcutKeyPressedCommand_Execute(object param) 
     { 
      //Find which key was presses by command param 
      int keyPressed = Int32.Parse((string)param); 

      // Do something bla bla bla 
     } 
: 백엔드에서

<!-- Set keybindings --> 
    <controls:MetroWindow.InputBindings> 
     <!-- NumPad Shortcuts for selecting reasons --> 
     <KeyBinding Key="NumPad0" Command="{Binding OnReasonShortcutKeyPressedCommand}" CommandParameter="0" /> 
     <KeyBinding Key="NumPad1" Command="{Binding OnReasonShortcutKeyPressedCommand}" CommandParameter="1" /> 
     <KeyBinding Key="NumPad2" Command="{Binding OnReasonShortcutKeyPressedCommand}" CommandParameter="2" /> 
     <KeyBinding Key="NumPad3" Command="{Binding OnReasonShortcutKeyPressedCommand}" CommandParameter="3" /> 
     <KeyBinding Key="NumPad4" Command="{Binding OnReasonShortcutKeyPressedCommand}" CommandParameter="4" /> 
     <KeyBinding Key="NumPad5" Command="{Binding OnReasonShortcutKeyPressedCommand}" CommandParameter="5" /> 
     <KeyBinding Key="NumPad6" Command="{Binding OnReasonShortcutKeyPressedCommand}" CommandParameter="6" /> 
     <KeyBinding Key="NumPad7" Command="{Binding OnReasonShortcutKeyPressedCommand}" CommandParameter="7" /> 
     <KeyBinding Key="NumPad8" Command="{Binding OnReasonShortcutKeyPressedCommand}" CommandParameter="8" /> 
     <KeyBinding Key="NumPad9" Command="{Binding OnReasonShortcutKeyPressedCommand}" CommandParameter="9" /> 

     <!-- Others --> 
     <KeyBinding Key="Back" Command="{Binding OnReasonGoBackClickedCommand}" /> 
     <KeyBinding Key="Escape" Command="{Binding OnEscapeClickedCommand}" /> 
    </controls:MetroWindow.InputBindings> 

을이 같이 처리됩니다

이제이 창에는 숫자를 입력해야하는 텍스트 상자가 포함됩니다. 물론 창 수준의 키 바인딩을 사용하면 실제 번호가 인쇄되는 대신 명령이 트리거됩니다. 이걸 무시할 수 있습니까?

답변

0

이 문제를 해결할 더 좋은 방법이 있는지 모르겠지만 첨부 파일 속성을 사용하여이를 보관하는 것이 좋습니다. 여기 은 예입니다

당신이 텍스트 상자에 새 동작 (LimitBindings)를 첨부해야한다 XAML에서

:이어야한다 - 그것은 부모 창을 얻을

<TextBox behavs:KeyBindingBehavior.LimitKeyBindings="True"></TextBox> 

과 당신의 행동 클래스 (+ 도우미 메서드를 만들 :
XAML을 다른 곳에서 :)) 위치 :

,691 :

public static class KeyBindingBehavior 
{ 
    //to keep window's bindings. 
    private static InputBindingCollection windowBindings; 

    public static bool GetLimitKeyBindings(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(LimitKeyBindingsProperty); 
    } 

    public static void SetLimitKeyBindings(DependencyObject obj, bool value) 
    { 
     obj.SetValue(LimitKeyBindingsProperty, value); 
    } 

    public static readonly DependencyProperty LimitKeyBindingsProperty = 
     DependencyProperty.RegisterAttached(
     "LimitKeyBindings", 
     typeof(bool), 
     typeof(KeyBindingBehavior), 
     new PropertyMetadata(false, PropertyChangedCallback)); 

    private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) 
    { 
     TextBox textBox = dependencyObject as TextBox; 
     if (textBox != null) 
     { 
      textBox.GotKeyboardFocus += textBox_GotKeyboardFocus; 
      textBox.LostKeyboardFocus += textBox_LostKeyboardFocus; 
     } 
    } 

    static void textBox_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e) 
    { 
     var window = GetParentWindow(sender as DependencyObject); 
     window.InputBindings.AddRange(windowBindings); 
    } 

    static void textBox_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e) 
    { 
     var window = GetParentWindow(sender as DependencyObject); 
     windowBindings = new InputBindingCollection(window.InputBindings); 
     window.InputBindings.Clear(); 
    } 

    // This helper method should be in seperate class 
    public static Window GetParentWindow(DependencyObject child) 
    { 
     DependencyObject parentObject = VisualTreeHelper.GetParent(child); 

     if (parentObject == null) 
     { 
      return null; 
     } 

     Window parent = parentObject as Window; 
     if (parent != null) 
     { 
      return parent; 
     } 
     else 
     { 
      return GetParentWindow(parentObject); 
     } 
    } 
} 

행동 클래스를 가리 키도록 XAML에 대한 참조를 추가하는 것을 잊지 마세요

xmlns:behavs="clr-namespace:WpfApplication1"