0
votes

I have a page with two text boxes, a check box and another two text boxes below at the bottom.

When I click on the check box, i want to set focus to the text box below it. This works fine, the soft-keyboard appears and the screen scrolls up to reveal the text box above the keyboard.

However, if either TextBox1 or TextBox2 is currently in focus and the keyboard is already showing, setting focus from the CheckBox_Click event will reset the screen and it scrolls back down.

I'm guessing that the events clash with one another - TextBox loses focus and hides the keyboard + scroll the screen back down - TextBox got focus from outside and shows the keyboard (but doesn't scroll the screen up)

Can this be prevented somehow?

Here's a simplified version of the xaml and code

The xaml:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <StackPanel Margin="0,100,0,0">
        <TextBox x:Name="TextBox1" />
        <TextBox x:Name="TextBox2" />
        <CheckBox Content="Click Me" Click="CheckBox_Click" />
        <TextBox x:Name="TextBox3" />
        <TextBox x:Name="TextBox4" />
    </StackPanel>
</Grid>

the code behind:

private void CheckBox_Click(object sender, RoutedEventArgs e)
{
    TextBox3.Focus();
}
1
Well, the first thing, that you should pay attention to: your CheckBox_click event doesn't control whether the checkBox is already checked or not. And I can't see, how does this happen: CheckBox_Click event will reset the screen and it scrolls back down Can't see any scrolling in your example. Didn't you miss something?Olter

1 Answers

0
votes

The problem you have is that the OS will try and move the page when the focus is on a control that isn't in a scrollable container and is obscured by the SIP.

I've worked around this by putting all the content in a ScrollViewer and adjusting the VerticalScrollOffset based on the selected control and if the SIP is shown or not. This was a lot of very messy, fiddely code. Ask yourself if the value of moving the focus is worth the effort before starting as there isn't a simple solution.