开发者

Software Keyboard Dissapears from screen when PasswordBox given focus

开发者 https://www.devze.com 2023-02-12 11:56 出处:网络
I have a PasswordBox on a page which I am trying to set to automatically have focus when navigating to the page.

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();
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消