2011-05-30 17 views
4

在我的代碼的的MainView,我有這個事件捉的keyDown:InputBinding代替PreviewKeyDown

#region Constructor 
PreviewKeyDown += SoftKeyMainPreviewKeyDown; 
#endregion 

private void SoftKeyMainPreviewKeyDown(object sender, KeyEventArgs e) 
{ 
     var focusedElement = Keyboard.FocusedElement; 
     switch (e.Key) 
     { 
      case Key.Up: 
       DoSomething(); 
       break; 
      ..... 
     } 
} 

現在我想移動到這一點在XAML化InputBindings。

我第一次嘗試:

<UserControl.InputBindings> 
    <KeyBinding Gesture="Up" Command="{Binding ElementName=_MaschinenView, Path=UpCommand}" /> 
    .... 
</UserControl.InputBindings> 

代碼隱藏:

public ICommand UpCommand { get; set; } 

UpCommand = new SimpleCommand { ExecuteDelegate = delegate { MoveFocusInDirection(Keyboard.FocusedElement, new TraversalRequest(FocusNavigationDirection.Up)); } }; 

有了這個什麼都不會發生。 KeyDown事件很可能是由一個子元素處理的。

是否可以在InputBindings的XAML中設置previewkeydown?如何實現這一目標?

回答

1

您是否嘗試過使用Key屬性而不是手勢?像這樣:

<UserControl.InputBindings> 
    <KeyBinding Key="Up" Command="{Binding ElementName=_MaschinenView, Path=UpCommand}" /> 
    .... 
</UserControl.InputBindings>