WPF:选项卡导航被折叠的超链接打断
问题:使用TAB键的导航会在折叠的文本块/超链接处停止 复制:WPF:选项卡导航被折叠的超链接打断,wpf,hyperlink,keyboard,navigation,visibility,Wpf,Hyperlink,Keyboard,Navigation,Visibility,问题:使用TAB键的导航会在折叠的文本块/超链接处停止 复制: <Window x:Class="TabTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="200" He
<Window x:Class="TabTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="200" Height="200">
<Grid>
<StackPanel Orientation="Vertical">
<TextBox Text="before" />
<TextBlock>
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</TextBlock.Style>
<Hyperlink Focusable="False">
<TextBlock Text="test" />
</Hyperlink>
</TextBlock>
<TextBox Text="after" />
</StackPanel>
</Grid>
</Window>
如果运行这个超级简单的演示并按TAB键,光标将移动到“之前”文本框。再次按TAB键会。。。没有什么。光标停留在“之前”文本框中,永远不会到达“之后”文本框。当超链接的文本块可见时,导航将按预期工作
问题:如何在超链接折叠的情况下使选项卡导航正常工作?问题不在于超链接,而在于文本块中的嵌套控件。你可以把它改成
<TextBlock Visibility="Collapsed">
<TextBlock Text="MyText" />
</TextBlock>
然后一切都按预期的方式进行。问题是,即使外部控件折叠,内部TextBlock也会获得焦点。将
KeyboardNavigation.TabNavigation
设置为一次
可以解决这个问题,因为整个容器及其子容器只获得一次焦点。()@Gimno的回答让我走上了正确的轨道,但我发现使用实际上只会让顶层元素聚焦一次(正如您从一次中所期望的那样)。Gimno的答案之所以有效,是因为他/她还在超链接上设置了Focusable=“False”
。如果TabNav=None,则不必在所有子控件上设置可聚焦
下面是我对该方法的应用(只有按钮获得选项卡焦点,而不是textblock或超链接):
浏览。。。
在我的例子中,即使是折叠的超链接,它也能工作。但是,当我使用FocusManager自动设置某些元素的焦点时,我遇到了类似的问题。StackPanel折叠时,使用Tab键阻止键盘导航。在我的情况下,我不得不删除FocusManager.FocusedElement,一切又恢复了正常
<StackPanel FocusManager.FocusedElement="{Binding ElementName=tbUser}"/>
也许这对其他人也有帮助,因为我花了一些时间来解决这个问题。简单但有效;-)非常感谢。
<Button Command="{Binding ChangeSoundCommand}" Click="ChangeSoundClick" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Padding="0"
KeyboardNavigation.TabNavigation="None">
<Button.Template>
<ControlTemplate>
<Grid>
<TextBlock Name="tb" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed" >
<Hyperlink>Browse...</Hyperlink>
</TextBlock>
<TextBlock Name="w_content" Text="{Binding FilePath}" TextTrimming="CharacterEllipsis" />
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="w_content" Property="Text" Value="">
<Setter TargetName="tb" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<StackPanel FocusManager.FocusedElement="{Binding ElementName=tbUser}"/>