一旦使用tab键进入toolbar,你会发现再也无法从toolbar里依靠tab键切换回来,特别是想回到menu目录项
如下是解决方案
仅仅需要加一个属性给toolbar
KeyboardNavigation.TabNavigation="Continue"
http://msdn.microsoft.com/zh-cn/library/system.windows.input.keyboardnavigation.tabnavigation.aspx
KeyboardNavigation..::.TabNavigation 附加属性
更新:2007 年 11 月
获取设置此属性所在元素的子级的逻辑 Tab 键导航行为或对该行为进行设置。
属性值
类型:System.Windows.Input..::.KeyboardNavigationMode
当发生 Tab 键导航时,容器移动焦点的方式。默认值为 Continue
***事实证明,toolbar默认值是cycle***
http://msdn.microsoft.com/zh-cn/library/system.windows.input.keyboardnavigationmode.aspx
成员
成员名称 | 说明 | |
---|---|---|
Continue | 对于每个元素,只要它是导航停止位,就可以接收键盘焦点。 当到达边缘时,导航会离开包含元素。 | |
Once | 容器及其所有子元素整个只能接收焦点一次。组中第一个子树或最后一个已设定焦点的元素会接收焦点 | |
Cycle | 根据导航方向,当到达容器的末尾或开始位置时,焦点将返回到第一项或最后一项。 焦点无法使用逻辑导航离开该容器。 | |
None | 不允许在此容器内进行键盘导航。 | |
Contained | 根据导航方向,当到达容器的末尾或开始位置时,焦点将返回到第一项或最后一项,而不会移至超出容器的开始或末尾的位置。 | |
Local | 只有 Tab 索引位于此容器内且其行为类似 Continue 时,才会在本地子树上考虑这些索引。 |
Members
Member name | Description | |
---|---|---|
Continue | Each element receives keyboard focus, as long as it is a navigation stop. Navigation leaves the containing element when an edge is reached. | |
Once | The container and all of its child elements as a whole receive focus only once. Either the first tree child or the or the last focused element in the group receives focus | |
Cycle | Depending on the direction of the navigation, the focus returns to the first or the last item when the end or the beginning of the container is reached. Focus cannot leave the container using logical navigation. | |
None | No keyboard navigation is allowed inside this container. | |
Contained | Depending on the direction of the navigation, focus returns to the first or the last item when the end or the beginning of the container is reached, but does not move past the beginning or end of the container. | |
Local | Tab Indexes are considered on local subtree only inside this container and behave like Continue after that. |
如下是我对toolbar该枚举进行简单测试的结果:
I have menu items, toolbar items and other 2 controls
Menu items is in main form and other 3 parts in contained form
Contained: My choice here, only cycle in menu items and toolbar items, started from menubar
Continue: Not well, all controls was in cycle and started from toolbar
Cycle: Default value for toolbar, only cycle in toolbar
Local: Looks just like contained, but i thought the behaviour decided by OS
None: Toolbar controls are ignored when tab change
Once: I thought it should only allow tab change once from toolbar, in fact it just looks like contained and local... Hard to understand
原文:
http://learnwpf.com/Posts/Post.aspx?postId=7c9b4f32-78c5-47d2-89e3-c6082a5d1d1b
One common way of moving the focus around in a user interface on Windows is to use the Tab key to move sequentially between controls. The contents of a WPF tool-bar seem to act like a kind of tabbing black-hole. The focus goes in there but never comes out again. Try pasting this Xaml into XamlPad to see the problem:
Xaml Code (.NET 3.0)
<DockPanel>
<ToolBar DockPanel.Dock="Top">
<Button Content="B"
Command="EditingCommands.ToggleBold" />
<Button Content="U"
Command="EditingCommands.ToggleUnderline" />
<Button Content="I"
Command="EditingCommands.ToggleItalic" />
</ToolBar>
<RichTextBox />
</DockPanel>
Fortunately this can be easily fixed by using the KeyboardNavigation attached property and changing the TabNavigation to "Continue". This allows the tab focus to move out of the toolbar and back to the other elements of the UI.
Xaml Code (.NET 3.0)
<DockPanel>
<ToolBar DockPanel.Dock="Top"
KeyboardNavigation.TabNavigation="Continue">
<Button Content="B"
Command="EditingCommands.ToggleBold" />
<Button Content="U"
Command="EditingCommands.ToggleUnderline" />
<Button Content="I"
Command="EditingCommands.ToggleItalic" />
</ToolBar>
<RichTextBox />
</DockPanel>