【发布时间】:2015-11-24 13:17:49
【问题描述】:
我想在“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>
问题是,我在我的代码隐藏中尝试了以下 sn-ps 的几种变体,它们可以很好地与普通 WPF 应用程序配合使用:
NumberEnterTextBox.Focus(FocusState.Pointer);
NumberEnterTextBox.Select(0, 0);
所以我的问题很简单:如何将焦点从我的代码隐藏设置到 NumberEnterTextBox?
【问题讨论】:
-
这基本上就是你会使用的(虽然我用过,.Focus(FocusState.Programmatic))。 Select(0, 0) 只会将光标位置设置为文本框的开头。我发现,取决于我将调用放在异步代码中的位置或与对话框相关的位置,可能会影响 Focus() 调用的效果。
-
您的代码 sn-p 在我身边有效。在使用 `FocusState.Pointer` 时,您将焦点设置为指针交互的直接结果。使用这段代码sn-p有什么问题?
标签: c# wpf windows-10 win-universal-app