防止在按左/右箭头键时焦点改变

4
我希望防止我的程序在按下左/右箭头键时更改焦点元素,因为我需要其他操作。现在,当在单击某个按钮以加载所有内容后按下这些键时,焦点会设置为该按钮旁边的组合框,这会防止键执行任何操作,而是更改组合框的选定索引。
以下是窗口上按键时的代码:
    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()并不能改变焦点,它仍然返回到组合框。

你有没有改变过 TabStop 属性? - Kcvin
3
我猜你需要设置 e.Handled = true;。 - Umesh
SystemOnline,问题解决了!谢谢。不确定为什么在发布前没有尝试过。 - Mohawk
1个回答

0

或许正在进行中

e.Handled = true;

就够了


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接