wpf 如何将光标焦点设置到文本框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22485650/
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-09-13 11:15:13  来源:igfitidea点击:

How to set cursor focus to a TextBox?

c#wpftextbox

提问by John Threepwood

How to set cursor focus to a TextBox?

如何将光标焦点设置到文本框?

I have a window pop up with a TextBoxand would like to focus the cursor to it. So a user can directly type text.

我有一个弹出的窗口,TextBox并希望将光标集中在它上面。所以用户可以直接输入文本。

I could not find a proper property. Is there one?

我找不到合适的财产。有吗?

回答by BRBT

To set focus on a textboxwhen your form loads you can do this:

textbox在表单加载时将焦点设置在 a 上,您可以执行以下操作:

private void Form_Load(object sender, EventArgs e)
    {
        SomeTextBox.Select();
    }

Note**You have to put it within the Form_Loadevent.

注意**你必须把它放在Form_Load事件中。

回答by Raging Bull

In WPF, try this:

在 WPF 中,试试这个:

FocusManager.SetFocusedElement(parentElement, txtMyTextBox)

Read more about FocusManager.SetFocusedElementhere.

FocusManager.SetFocusedElement在这里阅读更多信息。

OR

或者

txtMyTextBox.Focusable = true;
Keyboard.Focus(txtMyTextBox);