WPF中窗口元素上的TabNavigation

编程入门 行业动态 更新时间:2024-10-27 07:31:37
本文介绍了WPF中窗口元素上的TabNavigation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在基于WPF 4.0桌面的应用程序中,我想通过按Tab键来添加遍历窗口元素的功能.

In my WPF 4.0 desktop-based application, I want to add an ability to traverse through window elements by pressing Tab-button.

这是我的XAML的片段:

Here is fragment of my XAML:

<!--main body layout--> <StackPanel x:Name="BodyLayout" Style="{StaticResource Body_Block}"> <!--teaser block--> <Grid x:Name="TeaserGrid"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> </Grid.RowDefinitions> <TextBlock Grid.Column="0" Grid.Row="0" Style="{StaticResource Body_Teaser_Centering}"> <Hyperlink Style="{StaticResource Body_Teaser_Hyperlink}" Focusable="True" KeyboardNavigation.TabIndex="0" Click="Call_WinOffences_Click"> <Image Source="Resources/teaser_offences.png" Style="{StaticResource Body_Teaser_Image}" /> <LineBreak /> <TextBlock Text="Offences" Style="{StaticResource Body_Title}" /> </Hyperlink> </TextBlock> <TextBlock Grid.Column="1" Grid.Row="0" Style="{StaticResource Body_Teaser_Centering}"> <Hyperlink Style="{StaticResource Body_Teaser_Hyperlink}" Focusable="True" KeyboardNavigation.TabIndex="1" Click="Call_WinEvents_Click"> <Image Source="Resources/teaser_events.png" Style="{StaticResource Body_Teaser_Image}" /> <LineBreak /> <TextBlock Text="Events" Style="{StaticResource Body_Title}" /> </Hyperlink> </TextBlock> </Grid> </StackPanel>

我到底需要什么? 我要打开此窗口,并通过在Tab键盘上第一次按来在TabIndex="0"上将焦点设置在Hyperlink块(使用Image和TextBlock)上,并通过第二次按Tab来在焦点上使用TabIndex="1"来切换元素我想循环这个开关.换句话说,我希望用户可以像常规在其他任何WinForms应用程序中那样使用Tab键盘浏览窗口中的元素.

What exactly do I need? I want to open this window and by pressing first time on Tab keyboard to set focus on Hyperlink block (with Image and TextBlock) with TabIndex="0" and by second Tab pressing to switch focus on element with TabIndex="1", also I want to cycle this switches. In other words, I want that user could navigate through elements in my window by Tab keyboard as we are regular to do this in any other normal WinForms application.

我现在到底在做什么? 当我按下Tab键键盘时,超链接无法获得焦点,没有鼠标,我将无法使用我的窗口.

What actually have I right now? When I press on Tab keyboard Hyperlinks don't get focus and I can't work with my window without the mouse.

请让我知道我出了什么问题?

Please, let me know, what I'm going wrong?

推荐答案

更新 我将您的示例代码(除了我没有的样式)复制到一个小项目中,看来对我来说还不错.我可以启动Window,先按Tab键以首先关注超链接",再按Enter键,并引发Click事件,再按Tab键,依此类推.我在此处上传了示例项目.

Update I copied your sample code (except for the Styles which I didn't have) into a small project and it seems to be working fine for me. I can launch the Window, press Tab to Focus first Hyperlink, press Enter and the Click event is raised, press Tab again etc. The only thing I can think is that something in your Styles overrides this behavior. I uploaded my sample project here.

注意:我更改了图片的来源,因此您必须将它们改回:)

Note: I changed the Source for the Images so you'll have to change them back :)

旧帖子 这里有两个部分.第一个是FrameworkElement是否可聚焦.第二个是TabIndex. TabIndex在控件中,该控件从FrameworkElement派生,而并非所有元素都从Control派生,例如TextBlock,Rectangle等.如果出于某种原因也想向这些元素添加TabIndex,则可以改用KeyboardNavigation.TabIndex.这是一个简单的示例,其中包含6个具有Tab-Order设置的控件,而1个Rectangle不可聚焦(因为默认情况下不可聚焦).

Old post There are two parts here. The first one is if a FrameworkElement is Focusable or not. The second one is TabIndex. TabIndex is in Control which derives from FrameworkElement and not all elements derive from Control, such as TextBlock, Rectangle etc. If you for some reason want to add TabIndex to such elements as well you you can use KeyboardNavigation.TabIndex instead. Here's an easy example with 6 controls with a Tab-Order set and 1 Rectangle isn't Focusable (since it isn't Focusable by default).

<Grid ShowGridLines="True"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Button Grid.Row="0" Grid.Column="0" Content="Some Button" KeyboardNavigation.TabIndex="0" Margin="5"/> <TextBox Grid.Row="1" Grid.Column="0" Text="Some TextBox" KeyboardNavigation.TabIndex="2" Margin="5"/> <TextBox Grid.Row="2" Grid.Column="0" Text="Another TextBox" KeyboardNavigation.TabIndex="1" Margin="5"/> <TextBlock Grid.Row="0" Grid.Column="1" Text="Focusable TextBlock" Focusable="True" KeyboardNavigation.TabIndex="4" Margin="5"/> <Rectangle Grid.Row="1" Grid.Column="1" Fill="Blue" Margin="5"/> <Rectangle Grid.Row="2" Grid.Column="1" Fill="Red" Focusable="True" KeyboardNavigation.TabIndex="3" Margin="5"/> </Grid>

更多推荐

WPF中窗口元素上的TabNavigation

本文发布于:2023-11-06 16:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564192.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   窗口   WPF   TabNavigation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!