将焦点设置在 xaml wpf 中的文本框上 [英] Set the focus on a textbox in xaml wpf
问题描述
尽管这个论坛和其他一些帖子我找不到告诉我如何将焦点设置在 TextBox
上的内容.
Despite some posts on this forum and others i cannot find something that tells me how to set the focus on a TextBox
.
我有一个带有许多标签和文本框的用户控件.加载表单时,我希望特定的文本框具有焦点.
I have a userControl with many labels and textBoxes. When the form is loaded I want the a particular textBox to have the focus.
我已经设置了 tabIndex,但这似乎不起作用.
I have set the tabIndex but that didn't seem to work.
有什么建议吗?
推荐答案
您可以为此目的使用 FocusManager.FocusedElement
附加属性.这是一段默认将焦点设置为 TxtB 的代码.
You can use the FocusManager.FocusedElement
attached property for this purpose. Here's a piece of code that set the focus to TxtB by default.
<StackPanel Orientation="Vertical" FocusManager.FocusedElement="{Binding ElementName=TxtB}">
<TextBox x:Name="TxtA" Text="A" />
<TextBox x:Name="TxtB" Text="B" />
</StackPanel>
如果您不想在 XAML 中执行此操作,也可以在代码隐藏中使用 TxtB.Focus()
.
You can also use TxtB.Focus()
in your code-behind if you don't want to do this in XAML.
这篇关于将焦点设置在 xaml wpf 中的文本框上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!