为什么在 ScrollViewer 内部单击时我的 TextBox 会聚焦? [英] Why does my TextBox get focused when clicking inside of ScrollViewer?
问题描述
在我的 Windows Store 应用程序中,我创建了一个 ScrollViewer(里面有一个 Grid),里面有几个 TextBox.每当用户单击 ScrollViewer 中的任意位置时,第一个 TextBox 就会获得焦点.我不知道为什么会发生这种情况,这肯定不是我想要的行为.
In my Windows Store app, I've created a ScrollViewer (with a Grid inside) with a few TextBoxes inside. Whenever the user clicks anywhere within the ScrollViewer, the first TextBox is focused. I have no idea why this happens, and it certainly is not the behavior I want.
这只是 XAML 试图有用"的一种症状吗?我该如何预防?
Is this just a symptom of XAML trying to be "helpful"? How do I prevent it?
编辑:我找到了线索.这仅在我的 TextBox 位于 ScrollViewer 内时发生.它也出现在 C++ 和 C# 项目中,因此它显然是 XAML/WinRT 的症状.添加示例 XAML:
Edit: I found a clue. This only occurs when my TextBoxes are inside of a ScrollViewer. It also occurs on both C++ and C# projects, so it's obviously a symptom of XAML/WinRT. Adding example XAML:
使用以下 XAML,如果我聚焦第二个 TextBox,然后单击框之间边距中的任意位置,第一个 TextBox 将自动聚焦.我不想让它集中注意力.
With the following XAML, if I focus the second TextBox and then click anywhere in the margin between the boxes, the first TextBox is automatically focused. I don't want it to be focused.
<ScrollViewer Background="#111111"
VerticalScrollBarVisibility="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" />
<TextBox Grid.Row="1" />
</Grid>
</ScrollViewer>
推荐答案
找到解决方案!设置 ScrollViewer 的 TabStop="true"
可以防止这种行为.
Found a solution! Setting the ScrollViewer's TabStop="true"
prevents this behavior.
这篇关于为什么在 ScrollViewer 内部单击时我的 TextBox 会聚焦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!