简体   繁体   中英

Windows universal app set focus

I want to set the focus automatically when "NumberEnterTextBox" lost the focus. See my XAML code here:

<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Left">
    <TextBlock VerticalAlignment="Center" Text="Nummer eingeben: "/>
    <TextBox x:Name="NumberEnterTextBox" VerticalAlignment="Center" Width="200" HorizontalAlignment="Right" KeyUp="NumberEnterTextBox_KeyUp" TextChanging="NumberEnterTextBox_OnTextChanging"/>
</StackPanel>
<Viewbox x:Name="Viewbox" Grid.Row="1" HorizontalAlignment="Left">
        <Image Source="{Binding GetImageSource, Converter={StaticResource Converter}}" Stretch="Uniform"/>
</Viewbox>

Well the thing is, I tried several variations of the following snippets in my codebehind that works fine with a normal WPF application:

NumberEnterTextBox.Focus(FocusState.Pointer);
NumberEnterTextBox.Select(0, 0);

So my question is simple: How can I set the focus to the NumberEnterTextBox from my codebehind?

Well I have a solution, but I have no idea why this works in my case only within a LostFocus event:

private void NumberEnterTextBox_OnLostFocus(object sender, RoutedEventArgs e)
{
    NumberEnterTextBox.Focus(FocusState.Programmatic);
}

But thanks guys for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM