Software Keyboard Dissapears from screen when PasswordBox given focus
I have a PasswordBox on a page which I am trying to set to automatically have focus when navigating to the page.
I am having problems where when I give it focus programmatically, it accepts the focus, but the keyboard goes away.
This causes a problem as the user must click off the PasswordBox, and then back on to use the control.
I have tried adding this code in the page's Loaded event, the ContentGrid.Loaded, OnNavigatedTo, and they all produce the same result.
I ha开发者_如何学Cve tried setting the TabIndex/IsTabStop of the page, and the control itself, but it does not seem to work. The Passwordbox is the only item which has a TabIndex.
<PasswordBox x:Name="pwbAnswer" Style="{StaticResource PasswordBoxStyle}" VerticalAlignment="Top" Grid.Row="3"
PasswordChanged="pwbAnswer_PasswordChanged" KeyUp="pwbAnswer_KeyUp" TabIndex="1" IsTabStop="True" />
private void ContentGrid_Loaded(object sender, RoutedEventArgs e)
{
this.IsTabStop = true;
pwbAnswer.Focus();
}
You have to use the Loaded Event of the PasswordBox. I had the same problem. And than you can set the Focus to the sender, which is the PasswordBox itself, if you attached to that loading event.
<PasswordBox x:Name="pwbAnswer" Style="{StaticResource PasswordBoxStyle}" VerticalAlignment="Top" Grid.Row="3"
Loaded="PasswordBox_Loaded" PasswordChanged="pwbAnswer_PasswordChanged" KeyUp="pwbAnswer_KeyUp" TabIndex="1" IsTabStop="True" />
private void PasswordBox_Loaded(object sender, RoutedEventArgs e)
{
PasswordBox box = sender as PasswordBox;
box.Focus();
}
or you can use the workaround with the LayoutUpdated Event.
<Page .... LayoutUpdated="ContentGrid_LayoutUpdated">
<PasswordBox x:Name="pwbAnswer" Style="{StaticResource PasswordBoxStyle}" VerticalAlignment="Top" Grid.Row="3"
KeyUp="pwbAnswer_KeyUp" TabIndex="1" IsTabStop="True" />
private void ContentGrid_LayoutUpdated(object sender, RoutedEventArgs e)
{
this.IsTabStop = true;
pwbAnswer.Focus();
}
精彩评论