将焦点设置在xaml wpf中的文本框中 [英] Set the focus on a textbox in xaml wpf

查看:228
本文介绍了将焦点设置在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆