c# - 按下左/右键时防止焦点改变

标签 c# wpf 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() 不会改变焦点,它仍然返回到组合框。

最佳答案

也许在做

e.Handled = true;

就够了

关于c# - 按下左/右键时防止焦点改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22800845/

相关文章:

c# - 检查表是否存在 : Table doesn't exist while it exists

c# - 找到 "Debug assertion failed"错误发生的地方

wpf - 渐变透明系统颜色 (WPF)

wpf - WPF-将Window.Owner初始化为在其他线程上创建的Window

c# - 不支持的表达式类型 : System. Linq.Expressions.TypedParameterExpression

c# - 使用来自文本文件的参数运行 exe

wpf - wpf中区分鼠标双击和鼠标单击

css - 当用户从文本输入框点击链接时突出​​显示 anchor 链接

wpf - 如何在wpf中检查三个控件中的任何一个是否同时具有焦点?

reactjs - React 输入失去了对变化的关注