我希望防止我的程序在按下左/右箭头键时更改焦点元素,因为我需要其他操作。现在,当在单击某个按钮以加载所有内容后按下这些键时,焦点会设置为该按钮旁边的组合框,这会防止键执行任何操作,而是更改组合框的选定索引。
以下是窗口上按键时的代码:
但是无论我做什么都似乎无法防止焦点变化,而_ImageButton.Focus()并不能改变焦点,它仍然返回到组合框。
以下是窗口上按键时的代码:
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (!PostTypeComboBox.IsFocused && !PredefinedSubsComboBox.IsFocused && !SortComboBox.IsFocused)
{
switch (e.Key)
{
case System.Windows.Input.Key.Left:
e.Handled = false;
ViewModel.GoToPreviousPost();
PreviousImageButton.Focus();
break;
case System.Windows.Input.Key.Right:
e.Handled = false;
ViewModel.GoToNextPost();
NextImageButton.Focus();
break;
default:
break;
}
}
}
我已经尝试在switch语句中加入以下行和不加入以下行:
e.handled = False;
PreviousImageButton.Focus();
NextImageButton.Focus();
但是无论我做什么都似乎无法防止焦点变化,而_ImageButton.Focus()并不能改变焦点,它仍然返回到组合框。