Keyboard.Focus不会在WPF文本框中工作 [英] Keyboard.Focus does not work on text box in WPF

查看:234
本文介绍了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代码,启用,打可测试,tabstopable和可聚焦

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 =真实的,但并没有获得键盘焦点。
我缺少的东西吗?除非另一个控制保持凶猛抱到键盘焦点,我敢肯定,我didnt't代码,为什么会这样一段相当简单的代码无法正常工作。

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 didnt't code, why would this piece of rather simple code not work properly.

推荐答案

作为一种解决方法,你可以尝试使用调度在以后的的DispatcherPriority ,如输入

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.

您发布应正确设置焦点的代码,所以必须采取某种随后发生的移动键盘焦点出你的文本框的。在稍后调度优先级设置的焦点,你会确保键盘焦点设置为你的 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天全站免登陆