【问题标题】:Preventing focus change when left/right keys are pressed按下左/右键时防止焦点更改
【发布时间】:2014-04-08 00:19:42
【问题描述】:

我想阻止我的程序在按下左/右键时更改焦点元素,因为按下这些键时我需要发生其他事情。现在,当在单击某个按钮以加载所有内容后按下按键时,焦点将设置在该按钮旁边的组合框上,这会阻止按键执行任何操作,而是更改组合框的选定索引。

当在窗口上按下一个键时,我有这段代码:

    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 属性?
  • 我猜你需要设置 e.Handled = true;
  • SystemOnline,解决了问题!谢谢你。不知道为什么我在发布之前没有尝试过。

标签: c# wpf focus


【解决方案1】:

也许在做

e.Handled = true;

足够了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    相关资源
    最近更新 更多