ListBox映射J和K键向上/向下箭头键

时间:2013-07-07 17:04:11

标签: wpf

我有一个ListBox,我只想将J和K键绑定到上下箭头键绑定的任何命令。 WPF列表框中的向上和向下箭头键通常会将所选项目更改为上一个/下一个项目。我认为这样的事情应该有效:

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

我可能在这里过于简单了。

1 个答案:

答案 0 :(得分: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);
        }
    }
}