1

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

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

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

4

1 回答 1

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's:NextCommandPrevCommand. 还有一个 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);
        }
    }
}
于 2013-07-08T15:43:35.207 回答