设置文本框焦点的问题 [英] Issue with setting the focus for textbox
问题描述
我正在使用MVVM模式处理UWP应用程序,下面是我设置焦点的代码片段,但它没有将焦点设置为文本框。
XAML
< TextBox fontWeight设置= QUOT;粗体"字号= QUOT; 32" Text =" {Binding ProSeg1,Mode = TwoWay}" TextAlignment = QUOT;中心" MaxLength =" 3"
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; X:名称= QUOT; txtSeg" KeyUp =" txtSeg_KeyUp"
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Style =" {StaticResource textboxTemplate}" Width =" 105">< / TextBox>
USERCONTROL.XAML.CS -CONSTRUCTOR
bool val = txtSeg.Focus(FocusState.Keyboard); - 此代码从构造函数执行。
变量val始终返回false。在另一个实例中,相同的代码用于另一个文本框,但是它由按钮事件触发并且工作正常。
Anandan M
在以下网址中找到答案。
构造函数中的控件未就绪。因此在构造函数中调用Focus将不起作用。我们将调用用户控件的Loaded事件。
< span class ="kwd"style ="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant :inherit; font-weight:inherit; font-size:inherit;行高:继承; FONT-FAMILY:继承;垂直对齐:基线;颜色:#101094">这个 。 已加载 + = UserControl_Loaded ;
事件处理程序:私人 无效 UserControl_Loaded ( object 发件人 , RoutedEventArgs e )
{
txtSeg 。 焦点 ( FocusState 。 Programmatic );
}
Hi,
I am working with UWP app using MVVM pattern, below is my code snippet to set the focus, but it not setting the focus to textbox.
XAML
<TextBox FontWeight="Bold" FontSize="32" Text="{Binding ProSeg1, Mode= TwoWay}" TextAlignment="Center" MaxLength="3"
x:Name="txtSeg" KeyUp="txtSeg_KeyUp"
Style="{StaticResource textboxTemplate}" Width="105"></TextBox>
USERCONTROL.XAML.CS -CONSTRUCTOR
bool val= txtSeg.Focus(FocusState.Keyboard); - This code is executed from constructor.
variable val is always returned false. In another instance, the same code has used for another textbox, but it is triggered by a button event and it works fine.
Anandan M
Find the answer in the below URL.
Control is not ready in the constructor. So calling Focus in the constructor will not work. We would call in the Loaded event of the user control.
this.Loaded += UserControl_Loaded;
Event handler:
private void UserControl_Loaded(object sender, RoutedEventArgs e) { txtSeg.Focus(FocusState.Programmatic); }
这篇关于设置文本框焦点的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!