WPF:如何禁用选项卡导航而不同时禁用箭头键导航?
我已将窗口中所有控件的 IsTabStop
设置为 false,这样当我按 Tab 键时,焦点不会移动(我需要 Tab 键来执行其他操作)。但这样做会破坏箭头键导航 - 我单击 ListView 中的一个项目,然后按向上/向下键不会再更改所选项目。
有没有办法禁用选项卡导航,但不触摸箭头键导航?他们似乎有联系。
我尝试将 IsTabStop
设置为 true,将 TabNavigation
设置为 false,但它也不起作用。
<ListView ItemContainerStyle="{StaticResource ItemCommon}" IsTabStop="False">
<ListView.Resources>
<Style x:Key="ItemCommon">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle"/>
</Style>
</ListView.Resources>
</ListView>
I have set IsTabStop
to false on all controls in my window, so that when I press the Tab key, the focus doesn't move (I need the Tab key for something else). But doing this breaks arrow key navigation - I click on an item in a ListView
and then pressing up/down doesn't change the selected item anymore.
Is there a way to disable tab navigation, but without touching arrow key navigation? They seem to be related.
I tried setting IsTabStop
to true and TabNavigation
to false, but it doesn't work either.
<ListView ItemContainerStyle="{StaticResource ItemCommon}" IsTabStop="False">
<ListView.Resources>
<Style x:Key="ItemCommon">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle"/>
</Style>
</ListView.Resources>
</ListView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的窗口(或您不希望使用 Tab 键的控件的某些祖先)上吞下 Tab 键。
您可以通过附加到 PreviewKeyDown 事件并在按键为选项卡时设置 e.Handled = true 来吞下它。
纯代码隐藏:
您还可以这样设置键盘处理程序:
但您需要相应的事件处理程序:
On your window (or some ancestor of the controls you don't want tab to work on) swallow the tab key.
You can swallow it by attaching to the PreviewKeyDown event and set e.Handled = true when the key is a tab.
Pure Code Behind:
You can also set a Keyboard handler as such:
but you'll need a corresponding event handler:
我相信您想要的是将 ListView 上的 KeyboardNavigation.TabNavigation 附加属性设置为 Once 。我已经使用模板化的 ItemsControl 完成了此操作,它似乎给了我期望的行为,就像 ListBox 一样,其中进入控件的选项卡将选择第一个项目,但另一个选项卡将直接从列表框跳出并进入列表框。下一个控制。
因此,按照这种方法,您的示例可能可以缩短为这样。
我还没有使用 ListView 控件对此进行测试,但如果它适合您,我不会感到惊讶。
I believe what you want is to set the KeyboardNavigation.TabNavigation attached property to Once on your ListView. I've done this with a templated ItemsControl and it seems to give me the behavior that I would expect from like a ListBox where a tab into the control will select the first item but an additional tab will tab right out of the listbox and onto the next control.
So following this method your example may be able to be shortend down to just this.
I haven't tested this with the ListView control however but I wouldn't be surprised if it works for you.