2013-07-07 104 views
1

我有一個列表框,我只想將J和K鍵綁定到上下箭頭鍵綁定的任何命令。 WPF列表框中的向上和向下箭頭鍵通常會將所選項目更改爲上一個/下一個項目。我認爲這樣的事情應該可以工作:列表框地圖J和K鍵向上/向下箭頭鍵

<ListBox.InputBindings> 
    <KeyBinding Key="J" Command="ScrollBar.LineDownCommand" /> 
    <KeyBinding Key="K" Command="ScrollBar.LineUpCommand" /> 
    </ListBox.InputBindings> 

我可能在這裏太簡單了。

回答

1

您可以在命令上使用您的DependencyClass。在ListBox.InputBindings定義命令:

XAML

<ListBox Name="SampleListBox" Width="200" Height="200" KeyboardNavigation.DirectionalNavigation="Cycle" SelectedIndex="{Binding MySelectedIndex}"> 
    <ListBox.InputBindings> 
     <KeyBinding Command="{Binding NextCommand}" Gesture="CTRL+J" /> 
     <KeyBinding Command="{Binding PrevCommand}" Gesture="CTRL+K" /> 
    </ListBox.InputBindings> 

    <ListBoxItem>Sample 1</ListBoxItem> 
    <ListBoxItem>Sample 2</ListBoxItem> 
    <ListBoxItem>Sample 3</ListBoxItem> 
    <ListBoxItem>Sample 4</ListBoxItem> 
</ListBox> 

Code behind

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     // Set your data 
     this.DataContext = new MainWindowViewModel(); 

     // Set focus 
     SampleListBox.Focus(); 
    } 
} 

/// <summary> 
/// Class with commands 
/// </summary> 
public class MainWindowViewModel : DependencyObject 
{ 
    public ICommand NextCommand 
    { 
     get; 
     set; 
    } 

    public ICommand PrevCommand 
    { 
     get; 
     set; 
    } 

    public int MySelectedIndex 
    { 
     get 
     { 
      return (int)GetValue(MySelectedIndexProperty); 
     } 

     set 
     { 
      SetValue(MySelectedIndexProperty, value); 
     } 
    } 

    public static readonly DependencyProperty MySelectedIndexProperty = 
     DependencyProperty.Register("MySelectedIndex", typeof(int), typeof(MainWindowViewModel), new UIPropertyMetadata(0)); 

    public MainWindowViewModel() 
    { 
     MySelectedIndex = 0; 

     NextCommand = new SimpleCommand(SetNext); 
     PrevCommand = new SimpleCommand(SetPrev); 
    } 

    private void SetNext() 
    { 
     MySelectedIndex += 1; 
    } 

    private void SetPrev() 
    { 
     if (MySelectedIndex > 0) 
     { 
      MySelectedIndex -= 1; 
     } 
    } 
} 

public class SimpleCommand : ICommand 
{ 
    private Action _action; 

    public SimpleCommand(Action p_action) 
    { 
     _action = p_action; 
    } 

    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public event EventHandler CanExecuteChanged; 

    public void Execute(object parameter) 
    { 
     if (_action != null) 
     { 
      _action(); 
     } 
    } 
} 

在類包含兩個ICommand'sNextCommandPrevCommand。還有一個DependencyProperty MySelectedIndex,其中包含該項目的當前索引。在SimpleCommand總是返回true

這只是一個例子,仍然需要檢查總數Items ListBox。或者不要增加SelectedIndex,請使用ScrollViewer邏輯。

Extension

例與ScrollViewer

要通過在ListBox在項目之間滾動,您必須首先訪問它。下面是相應的功能:

public static DependencyObject GetScrollViewer(DependencyObject Object) 
{ 
    if (Object is ScrollViewer) 
    { 
     return Object; 
    } 

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(Object); i++) 
    { 
     var child = VisualTreeHelper.GetChild(Object, i); 
     var result = GetScrollViewer(child); 

     if (result == null) 
     { 
      continue; 
     } 
     else 
     { 
      return result; 
     } 
    } 

    return null; 
} 

簡單的函數滾動:

private void OnScrollDown(object sender, RoutedEventArgs e) 
{ 
    if (MyListBox.Items.Count > 0) 
    { 
     // Get ScrollViewer from ListBox 
     ScrollViewer scrollViewer = GetScrollViewer(MyListBox) as ScrollViewer; 

     if (scrollViewer != null) 
     { 
      // Increment offset - scrolling Down, sub - scrolling Up 
      scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + ScrollListBoxOffset); 
     } 
    } 
} 
+0

+1的努力。 ListBox已經具有滾動/選擇行爲,我只想將它連接到上/下箭頭鍵。我原以爲會有一種方法來解決這個問題並將它連接到J/K鍵。不得不自己實施這個行爲只是爲了選擇不同的鍵,似乎有點跛腳(不是你,WPF)。在這個和愚蠢的焦點問題之間,我準備放棄WPF。 –

相關問題