[英]WPF - set focus on textbox
在WPF
应用程序中,一页上有很多文本框。 我想将焦点放在第一个文本框上。 我已经用谷歌搜索并尝试了不同的解决方案。
XAML代码:
<StackPanel Width="350" HorizontalAlignment="Left" >
<TextBlock x:Name="CustNamelbl" Text="C U S T O M E R N A M E"
Style="{StaticResource LightBoldTxtblkStyle }"/>
<Grid Height="35">
<TextBox x:Name="CustName" Style="{StaticResource Txtbox}"
LostFocus="CustName_LostFocus_1" TabIndex="1"
CommandManager.PreviewExecuted="CustName_PreviewExecuted"
ContextMenu="{x:Null}" PreviewTextInput="CustName_PreviewTextInput"
Margin="0,0,0,0" GotFocus="CustName_GotFocus"/>
<Rectangle Fill="White" Height="2" Opacity="0.2"
VerticalAlignment="Bottom"></Rectangle>
</Grid>
</StackPanel>
C#代码:
CustName.Focus();
Keyboard.Focus(CustName);
使用此代码,我将Focus
放在该TextBox
。 我也可以打开系统键盘。 但是,我无法在该TextBox
键入任何内容。 另外,该焦点光标不是闪烁的光标,而是稳定的光标。
给您的TextBox
一个名字,然后您可以在窗口标签中调用
FocusManager.FocusedElement="{Binding ElementName=YOURTBNAME}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.