3

当我登录到我的应用程序时如何设置焦点,当登录窗口打开时我想在不使用鼠标的情况下输入密码

<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"> 
4

2 回答 2

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>
于 2016-04-04T13:29:11.203 回答
0

解决方案:

passwordbox.Focus();

或在 .xaml 文件中在元素中包含以下内容作为其属性。

FocusManager.FocusedElement="{Binding ElementName=passwordbox}">

笔记:

如果您想决定在窗体首次出现时将焦点放在哪个控件上,请将代码放在 Loaded 事件中。

Loaded += (o, e) => {
  passwordbox.Focus();
};
于 2019-04-25T09:52:23.057 回答