Keyboard.Focus 不适用于 WPF 中的文本框 [英] Keyboard.Focus does not work on text box in WPF

查看:24
本文介绍了Keyboard.Focus 不适用于 WPF 中的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决在 wpf 中看起来如此简单的问题,但我还没有发现为什么我不能让我的应用程序按照我的计划运行.

I am banging my head on what looks like such a simple problem to fix in wpf but i have yet to discover why i can't get my app to behave according to my plan.

当用户按下 ctrl+f 时,我的 wpf 应用程序中会弹出一个小的搜索框.我想要的只是插入符号在搜索框文本框中闪烁,准备好接受任何用户输入,而无需用户单击它.这是文本框的 xaml 代码,它是可见的、启用的、可点击测试的、可制表符的和可聚焦的.

I have a small search box that pops-up in my wpf application when user presses ctrl+f. All i want is for the caret to be flashing inside the search box text box, ready to take whatever user input without the user having to click on it. Here is the xaml code for the text box which is visible, enabled, hit testable, tabstopable and focusable.

   <TextBox x:Name="SearchCriteriaTextBox" Text="{Binding SearchCriteria}" Focusable="True" IsEnabled="True" IsTabStop="True" IsHitTestVisible="True" Style="{DynamicResource SearchTextBoxStyle}" Grid.Column="1" Margin="5,10,0,5" />

在后面的代码中,当搜索框的可见性受到影响时,我会调用此方法.搜索框在应用启动时加载.

In the code behind, i have this method called when the visibility of the search box is affected. the search box is loaded at the start of the app.

    /// <summary>
    /// Handles events triggered from focusing on this view.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="dependencyPropertyChangedEventArgs">The key event args.</param>
    private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        if (!((bool) dependencyPropertyChangedEventArgs.NewValue))
        {
            return;
        }

        SearchCriteriaTextBox.Focus();
        Keyboard.Focus(SearchCriteriaTextBox);
        SearchCriteriaTextBox.Select(0, 0);

        if (SearchCriteriaTextBox.Text.Length > 0)
        {
            SearchCriteriaTextBox.SelectAll();
        }
    }

问题是,代码被调用,组件变为 IsFocused=true 但没有获得键盘焦点.我错过了什么吗?除非另一个控件一直保持在我很确定我没有编码的键盘焦点上,否则为什么这段相当简单的代码不能正常工作.

The problem is, code gets called, component becomes IsFocused=true but does not gain keyboard focus. Am I missing something? Unless another control keeps hold ferociously to the keyboard focus which i am pretty sure i didn't code, why would this piece of rather simple code would not work properly.

推荐答案

作为一种解决方法,您可以尝试使用 Dispatcher 稍后设置焦点 DispatcherPriority,如Input

As a workaround, you could try using the Dispatcher to set the focus at a later DispatcherPriority, such as Input

Dispatcher.BeginInvoke(DispatcherPriority.Input,
    new Action(delegate() { 
        SearchCriteriaTextBox.Focus();         // Set Logical Focus
        Keyboard.Focus(SearchCriteriaTextBox); // Set Keyboard Focus
     }));

从您的问题描述来看,您似乎没有设置键盘焦点.WPF 可以有多个焦点范围,因此多个元素可以有逻辑焦点 (IsFocused = true),但是只有一个元素可以有键盘焦点并接收键盘输入.

From the description of your problem, it sounds like you don't have Keyboard focus set. WPF can have multiple Focus Scopes, so multiple elements can have Logical Focus (IsFocused = true), however only one element can have Keyboard Focus and will receive keyboard input.

您发布的代码应该正确设置焦点,因此之后必须发生某些事情才能将键盘焦点移出 TextBox.通过将焦点设置为稍后的调度程序优先级,您将确保将键盘焦点设置到 SearchCriteriaTextBox 最后完成.

The code you posted should set the focus correctly, so something must be occurring afterwards to move Keyboard Focus out of your TextBox. By setting focus at a later dispatcher priority, you'll be ensuring that setting keyboard focus to your SearchCriteriaTextBox gets done last.

这篇关于Keyboard.Focus 不适用于 WPF 中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆