2017-01-28 1 views
0

사용자가 listview 내에서 이동하려고 위/아래를 눌렀을 때 listview에 포커스가 없다고 생각하고 싶습니다. 그러나 사용자가 텍스트 상자에 입력하는 경우 목록보기에서 더 이상 탐색하지 않으려면 아래로 누릅니다. 가능한 해결 방법은 모든 요소에 대해 PreviewKeyDown 이벤트를 추가하고 캡처 된 키가 위/아래 인 경우 트리 아래로 전달하지만이 솔루션은 많은 요소가 있으므로 매우 실용적이지는 않습니다.WPF - 초점이 맞지 않아도 컨트롤에 의해 캡처되는 키보드 단축키

예제 코드 :

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     PreviewKeyDown += (s, e) => 
     { 
      var viewModel = DataContext as YourViewModel; 
      if(viewModel != null) 
      { 
       if (e.Key == System.Windows.Input.Key.Up) 
       { 
        viewModel.upCommand.Execute(null); 
        e.Handled = true; 
       } 
       else if(e.Key == System.Windows.Input.Key.Down) 
       { 
        viewModel.downCommand.Execute(null); 
        e.Handled = true; 
       } 
      } 

     }; 
} 

그런 다음 당신은 다른 요소를 처리 할 필요가 없습니다

<StackPanel> 
    <ListView x:Name="capturesUpDownWhenTextBoxNotFocused" ItemsSource="{Binding list}" ItemTemplate="{StaticResource template}"> 
     <ListView.InputBindings> 
      <KeyBinding Key="Up" Command="{Binding upCommand}"></KeyBinding> 
      <KeyBinding Key="Down" Command="{Binding downCommand}"></KeyBinding> 
     </ListView.InputBindings> 
    </ListView> 
    <TextBox Text="random text"></TextBox> 
    <Button Content="button"></Button> 
    <ListView x:Name="doesNotCaptureUpDownEvenIfFocused" ItemsSource="{Binding activeFile.activeFilters}" ItemTemplate="{StaticResource template}"></ListView> 
</StackPanel> 

답변

1

당신은 부모 윈도우의 PreviewKeyDown 이벤트를 처리 할 수있다.

아니요, 여기에는 순수한 XAML 솔루션이 없습니다.

+0

옙 내가 인터넷 검색을 한 후에 찍은 경로입니다. 답변을 주셔서 감사합니다. 이것이 좋은 접근 방법임을 확인했습니다. –

관련 문제