Wpf 列表框将J和K键映射到上/下箭头键
我有一个列表框,我只想将J键和K键绑定到上下箭头键绑定到的任何命令。WPF列表框中的向上和向下箭头键通常会将所选项目更改为上一个/下一个项目。我觉得这样应该行得通:Wpf 列表框将J和K键映射到上/下箭头键,wpf,Wpf,我有一个列表框,我只想将J键和K键绑定到上下箭头键绑定到的任何命令。WPF列表框中的向上和向下箭头键通常会将所选项目更改为上一个/下一个项目。我觉得这样应该行得通: <ListBox.InputBindings> <KeyBinding Key="J" Command="ScrollBar.LineDownCommand" /> <KeyBinding Key="K" Command="ScrollBar.LineUpCommand" />
<ListBox.InputBindings>
<KeyBinding Key="J" Command="ScrollBar.LineDownCommand" />
<KeyBinding Key="K" Command="ScrollBar.LineUpCommand" />
</ListBox.InputBindings>
我可能太简单了。您可以在命令上使用
DependencyClass
。在列表框中定义命令。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>
类中包含两个ICommand
:NextCommand
和PrevCommand
。还有一个dependencPropertyMySelectedIndex
,它包含项目的当前索引。在SimpleCommand
中,始终返回true
这只是一个仍然需要检查项目列表框的总数的示例。或者使用ScrollViewer
逻辑,而不是增加SelectedIndex
分机
ScrollViewer的示例:
要滚动查看列表框中的项目,必须首先访问该列表框。以下是相应的功能:
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;
}
您可以在命令上使用DependencyClass
。在列表框中定义命令。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>
类中包含两个ICommand
:NextCommand
和PrevCommand
。还有一个dependencPropertyMySelectedIndex
,它包含项目的当前索引。在SimpleCommand
中,始终返回true
这只是一个仍然需要检查项目列表框的总数的示例。或者使用ScrollViewer
逻辑,而不是增加SelectedIndex
分机
ScrollViewer的示例:
要滚动查看列表框中的项目,必须首先访问该列表框。以下是相应的功能:
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;
}
+1.谢谢你的努力。列表框已经有了我想要的滚动/选择行为,只是它连接到了上/下箭头键。我本以为会有一种方法来获得这种行为并将其连接到J/K键。仅仅为了选择不同的键而不得不自己实现这个行为似乎有点蹩脚(不是你,WPF)。在这和愚蠢的焦点问题之间,我准备放弃WPF.+1的努力。列表框已经有了我想要的滚动/选择行为,只是它连接到了上/下箭头键。我本以为会有一种方法来获得这种行为并将其连接到J/K键。仅仅为了选择不同的键而不得不自己实现这个行为似乎有点蹩脚(不是你,WPF)。在这和愚蠢的焦点问题之间,我准备放弃WPF。