问题描述
我要重写列表框项目的选择行为。
I want to override the selection behavior of ListBox Items.
我们最多可以使用通过遍历ListBox中的项目和向下箭头,但我想用左遍历列表右箭头键。
We can traverse through ListBox items using Up and Down arrows but I want to traverse the list using Left and Right arrow keys.
虽然我尝试添加的键按下事件列表框,它显示除了方向键,Home,End和类似的按键几乎所有的按键。
While I am trying to add the key down event for ListBox, It shows almost all key presses except for Arrow Keys, Home, End and similar keys.
我有以下代码:
private void listArtist_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key.ToString() == "Enter")
{
// Go to some page
}
else
{
MessageBox.Show(e.Key.ToString());
}
}
我茫然不知这一点。请帮助。
I am clueless about this. Please help.
谢谢,
Subhen
Thanks,Subhen
推荐答案
你需要做的是子类ListBox和重写的onkeydown功能。也许是这样的:
What you need to do is subclass ListBox and override the OnKeyDown function. Maybe something like this:
class MyListBox : ListBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.Key)
{
case Key.Left:
// ....
e.Handled = true;
break;
case Key.Right:
// ....
e.Handled = true;
break;
}
if (!e.Handled)
base.OnKeyDown(e);
}
}
这篇关于KeyDown事件不工作方向键,Home,End,PageUp键,PageDown键和其他在Silverlight类似钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!