我想阻止我的程序在按下左/右键时更改焦点元素,因为我需要在按下这些键时发生其他事情。现在,当点击某个按钮加载所有内容后按下按键时,焦点将设置到该按钮旁边的组合框,这会阻止按键执行任何操作,而是更改组合框的选定索引。
当在窗口上按下一个键时,我有这段代码:
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() 不会改变焦点,它仍然返回到组合框。
最佳答案
也许在做
e.Handled = true;
就够了
关于c# - 按下左/右键时防止焦点改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22800845/