简体   繁体   English

WPF / C中的文本框上的光标聚焦#

[英]Cursor Focus on Textbox in WPF/C#

I am currently in the process of creating a Onscreen keyboard. 我目前正在创建一个屏幕键盘。 I am handling the button click using routedcommands. 我正在使用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. 是否有一种方法可以在单击按钮时将焦点保持在文本框上。

To set logical focus to an input control 将逻辑焦点设置为输入控件

FocusManager.SetFocusedElement(this, textboxJack);     // 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 - Focus on MSDN 输入概述 - 专注于MSDN

Get the reference for the specific control(in that case TextBox). 获取特定控件的参考(在这种情况下为TextBox)。 After click, in Button_Click method paste this: 单击后,在Button_Click方法中粘贴此:

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

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;
}
Textbox.Focus();

这将集中在文本框上

The way I solved this problem was to set focusable=false to all the buttons/controls on the keyboard. 我解决这个问题的方法是将focusable=false设置为键盘上的所有按钮/控件。 That way you don't lose focused of the current control. 这样你就不会失去当前控制的焦点。

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 }
);

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"

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. 当我正在使用MVVM时,我真的需要一个纯粹在XAML中工作的答案。

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}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM