当我登录我的应用程序时如何设置焦点,当登录窗口打开时我想在不使用鼠标的情况下输入密码
<PasswordBox x:Name="passwordbox" Grid.Row="1" Grid.Column="1" Margin="5,35,5,5" Width="280" Height="27" app:PasswordBoxAssistant.BindPassword="true" app:PasswordBoxAssistant.BoundPassword="{Binding TechUserModel.UserId,Mode=TwoWay,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center">
答案 0 :(得分:2)
我已尝试过针对此场景的各种解决方案,我发现最有效的方法是使用FocusManager.FocusedElement:
<Window x:Class="StackOverflow.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test" Height="300" Width="300"
FocusManager.FocusedElement="{Binding ElementName=Box}">
<Grid>
<TextBox x:Name="Box"/>
</Grid>
</Window>
答案 1 :(得分:0)
解决方案:
passwordbox.Focus();
或.xaml文件中的元素包括以下内容作为其属性。
FocusManager.FocusedElement="{Binding ElementName=passwordbox}">
注意:
如果您要确定表单首次出现时要重点关注的控件,请将代码放入Loaded事件中。
Loaded += (o, e) => {
passwordbox.Focus();
};