在 WPF/C# 中光标聚焦于文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/828155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:15:31  来源:igfitidea点击:

Cursor Focus on Textbox in WPF/C#

c#wpftextbox

提问by

I am currently in the process of creating a Onscreen keyboard. I am handling the button click using routedcommands. The issue is that when i click on the button in keyboard panel the focus shifts to the button rather than on the Textbox. The requirement states that a cursor should always appear in the text box to indicate the position where the next character will be inserted. Is there a way i can keep focus on the textbox while button is clicked.

我目前正在创建屏幕键盘。我正在使用路由命令处理按钮点击。问题是,当我单击键盘面板中的按钮时,焦点会转移到按钮而不是文本框上。该要求规定光标应始终出现在文本框中以指示将插入下一个字符的位置。有没有办法可以在单击按钮时将注意力集中在文本框上。

回答by Gishu

To set logical focus to an input control

将逻辑焦点设置到输入控件

FocusManager.SetFocusedElement(this, textboxHyman);     // set logical focus

To set keyboard focus to an input control

将键盘焦点设置到输入控件

Keyboard.Focus(textboxJill);                             // set keyboard focus

To know the difference between logical and keyboard focus

了解逻辑焦点和键盘焦点之间的区别

Input Overview - Focuson MSDN

输入概述 - 专注于 MSDN

回答by Josh F.

I like these do-my-homework-for-me questions; "the requirement states"...priceless. For those who find this via Google, the trick to progmatically moving the cursor in a WPF TextBox is to use the SelectioNStart property.

我喜欢这些为我做功课的问题;“要求说明”……无价。对于那些通过 Google 找到它的人,在 WPF TextBox 中以编程方式移动光标的技巧是使用 SelectioNStart 属性。

private void Button_Click(object sender, RoutedEventArgs e)
{
    textBox.Focus();
    textBox.SelectionStart = textName.Text.Length;
}

回答by Jonas Stawski

The way I solved this problem was to set focusable=falseto all the buttons/controls on the keyboard. That way you don't lose focused of the current control.

我解决这个问题的方法是设置focusable=false键盘上的所有按钮/控件。这样你就不会失去对当前控制的关注。

回答by Xiaosu

Your problem can be solved by using a separate focus scope for your "keyboard". Just apply the following property to the control that contains all of your buttons and then they will be in a separate focus scope and will not have the focus set to them when clicked

您的问题可以通过为“键盘”使用单独的焦点范围来解决。只需将以下属性应用于包含所有按钮的控件,然后它们将位于单独的焦点范围内,并且在单击时不会将焦点设置为它们

FocusManager.IsFocusScope="True"

回答by gokul

Textbox.Focus();

this will focus on the textbox

这将专注于文本框

回答by mashet

Get the reference for the specific control(in that case TextBox). After click, in Button_Click method paste this:

获取特定控件的引用(在这种情况下为 TextBox)。单击后,在 Button_Click 方法中粘贴:

Dispatcher.BeginInvoke((ThreadStart)delegate
            {
                control.Focus();
            });

回答by Brendan Botond

I had to use this to get my desired result

我不得不用它来获得我想要的结果

FocusManager.SetFocusedElement(this, UserNameautoCompleteBox);

Key key = Key.Enter;                    // Key to send
var target = Keyboard.FocusedElement;    // Target element
RoutedEvent routedEvent = Keyboard.KeyDownEvent; // Event to send

target.RaiseEvent(
    new KeyEventArgs(
        Keyboard.PrimaryDevice,
        PresentationSource.FromVisual(UserNameautoCompleteBox),
        0,
        key) { RoutedEvent = routedEvent }
);

回答by Anya Hope

I wanted to do the same thing, but nothing seemed to work for me. I really needed an answer that worked purely in XAML as I'm working with MVVM.

我想做同样的事情,但似乎没有什么对我有用。我真的需要一个纯粹在 XAML 中工作的答案,因为我正在使用 MVVM。

I finally found this example: http://spin.atomicobject.com/2013/03/06/xaml-wpf-textbox-focus/

我终于找到了这个例子:http: //spin.atomicobject.com/2013/03/06/xaml-wpf-textbox-focus/

and modified it to this:

并将其修改为:

In the 'Resources' section:

在“资源”部分:

    <Style x:Key="FocusTextBox" TargetType="Grid">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=textBoxName, Path=IsVisible}" Value="True">
                <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=textBoxName}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

In my grid definition:

在我的网格定义中:

<Grid Style="{StaticResource FocusTextBox}" />