将焦点设置在xaml wpf中的文本框中 [英] Set the focus on a textbox in xaml wpf
问题描述
TextBox
。 我有一个userControl与许多标签和文本框。当表单被加载时,我想要一个特定的文本框有焦点。
我已经设置了tabIndex,但似乎没有工作。 b
$ b
有什么建议?
您可以使用 FocusManager。 FocusedElement
附属属性为此目的。
< StackPanel Orientation =VerticalFocusManager.FocusedElement = {Binding ElementName = TxtB}>
< TextBox x:Name =TxtAText =A/>
< TextBox x:Name =TxtBText =B/>
< / StackPanel>
您也可以使用 TxtB.Focus()
在你的代码后面,如果你不想在XAML中做到这一点。
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.
I have set the tabIndex but that didn't seem to work.
Any suggestions?
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>
You can also use TxtB.Focus()
in your code-behind if you don't want to do this in XAML.
这篇关于将焦点设置在xaml wpf中的文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!