.net 在 WPF 中设置 Tab 键顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/359758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:48:08  来源:igfitidea点击:

Setting tab order in WPF

.netwpftab-orderingkeyboard-navigation

提问by Román

How do I set tab ordering in WPF? I have an ItemsControl with some items expanded and some collapsed and would like to skip the collapsed ones when I'm tabbing.

如何在 WPF 中设置选项卡顺序?我有一个 ItemsControl,其中一些项目已展开,一些项目已折叠,并且希望在我使用 Tab 键时跳过折叠的项目。

Any ideas?

有任何想法吗?

采纳答案by Jab

You can skip elements in the tab sequence by setting KeyboardNavigation.IsTabStopon the element in XAML.

您可以通过在 XAML 中的元素上设置KeyboardNavigation.IsTabStop来跳过选项卡序列中的元素。

KeyboardNavigation.IsTabStop="False"

You can setup a trigger that would toggle this property based on the expanded state.

您可以设置一个触发器,根据展开状态切换此属性。

回答by Drew Noakes

If you want to explicitly set the tab ordering for elements in your form, the following attached property is supposed to help:

如果您想为表单中的元素显式设置选项卡顺序,以下附加属性应该会有所帮助:

<Control KeyboardNavigation.TabIndex="0" ... />

I say "supposed to help" as I haven't found it very reliable though I probably need to read more about how it is intended to be used. I only post this half baked answer because no one else mentioned this property.

我说“应该有帮助”,因为我还没有发现它非常可靠,尽管我可能需要阅读更多关于它的用途的信息。我只发布这个半成品的答案,因为没有人提到这个属性。



Note that in Win RT, the property is just TabIndex="0".

请注意,在 Win RT 中,该属性仅为TabIndex="0".

回答by Pankaj

<Control KeyboardNavigation.TabIndex="0" ... />Works perfectly fine... For example-

<Control KeyboardNavigation.TabIndex="0" ... />工作得很好......例如-

<ComboBox Height="23" 
          Margin="148,24,78,0" 
          Name="comboBoxDataSet"
          VerticalAlignment="Top"
          SelectionChanged="comboBoxDestMarketDataSet_SelectionChanged"
          DropDownOpened="comboBoxDestMarketDataSet_DropDownOpened"
          KeyboardNavigation.TabIndex="0" />
<ComboBox Height="23" 
          Margin="148,56,78,0" 
          Name="comboBoxCategory" 
          VerticalAlignment="Top" 
          SelectionChanged="comboBoxDestCategory_SelectionChanged"
          DropDownOpened="comboBoxDestCategory_DropDownOpened"
          KeyboardNavigation.TabIndex="1" />

Will allow you to navigate through these two combo boxes using TAB key.

将允许您使用 TAB 键浏览这两个组合框。

回答by AltF4_

I think there is a much easier solution here, at the top within your control or window or whatever, you could add:

我认为这里有一个更简单的解决方案,在您的控件或窗口或其他任何内容的顶部,您可以添加:

KeyboardNavigation.TabNavigation="Cycle"

This also automaticaly ignores the collapsed tabs.

这也会自动忽略折叠的选项卡。

回答by Gustavo Mori

Another alternative that has worked for me in the past is to simply remove all explicit TabIndexstatements, and let the controls use the order that they're declared in XAML work their magic.

过去对我TabIndex有用的另一种选择是简单地删除所有显式语句,让控件使用它们在 XAML 中声明的顺序发挥它们的魔力。

This, of course, may require you to reorder your controls. But this is a simple copy-paste operation.

当然,这可能需要您对控件重新排序。但这是一个简单的复制粘贴操作。