Windows通用应用程序集焦点

本文关键字:程序集 焦点 应用程序 应用 Windows | 更新日期: 2023-09-27 18:24:02

当"NumberEnterTextBox"失去焦点时,我想自动设置焦点。请在此处查看我的XAML代码:

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

问题是,我在我的代码库中尝试了以下片段的几种变体,这些片段可以很好地与普通的WPF应用程序配合使用:

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

所以我的问题很简单:如何从我的代码后台将焦点设置为NumberEnterTextBox?

Windows通用应用程序集焦点

我有一个解决方案,但我不知道为什么这只在LostFocus事件中有效:

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

但是谢谢你们的帮助!