使 WPF TabControl 忽略 Ctrl+Tab
我正在编写一个使用“选项卡式浏览”比喻的应用程序,其中 TabControl 为窗口的完整大小,以及选项卡内的其他内容。 有时,这些选项卡本身会包含其他 TabControl。
(选项卡内的选项卡可能会令人困惑,因此我将重新设置内部 TabControl 的样式,使其看起来不像 TabControl。我可能会将其样式设置为使用顶部的 ToggleButtons 而不是选项卡。)
我希望此 UI行为就像您期望选项卡式浏览隐喻一样工作: Ctrl+Tab 应始终在外部 TabControl(看起来像 TabControl 的那个)上切换选项卡,即使键盘焦点位于内部 TabControl 内部(它看起来不像 TabControl,因此不应期望响应 Ctrl+Tab)。 但是,当然,内部 TabControl 首先获取按键事件并自行处理。
阻止内部 TabControl 响应 Ctrl+Tab 和 Ctrl+Shift+< 的最佳方法是什么kbd>Tab 键事件,那么这些事件可以冒泡到外部 TabControl 吗?
I'm writing an app that uses the "tabbed browsing" metaphor, with a TabControl the full size of the window, and other stuff inside the tabs. Sometimes those tabs will themselves contain other TabControls.
(Tabs inside tabs can be confusing, so I'll re-style the inner TabControl so it doesn't look like a TabControl. I'll probably style it to use ToggleButtons at the top instead of tabs.)
I want this UI to behave like you would expect the tabbed-browsing metaphor to work: Ctrl+Tab should always switch tabs on the outer TabControl (the one that looks like a TabControl), even if keyboard focus is inside the inner TabControl (which doesn't look like a TabControl, and therefore shouldn't be expected to respond to Ctrl+Tab). But, of course, the inner TabControl gets the key event first and handles it itself.
What's the best way to keep the inner TabControl from responding to the Ctrl+Tab and Ctrl+Shift+Tab key events, so those events can bubble up to the outer TabControl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WPF TabControl 似乎通过 OnKeyDown 方法管理键盘导航功能。 我建议创建一个继承自 TabControl 的自定义控件,并重写 OnKeyDown 方法。
The WPF TabControl appears to manage the keyboard navigation feature via the OnKeyDown method. I would suggest creating a custom control that inherits from the TabControl, and override the OnKeyDown method.
您可以处理内部 TabControl 上的 PreviewKeyDown 事件并设置 e.Handled = true 以防止其处理按键事件。 然后,您可以找到父 TabControl (可能通过 ((TabControl)sender).Parent 递归地找到)并以编程方式更改其 SelectedIndex。
将其包装在自定义控件中将使其保持相当干净。
You can handle the PreviewKeyDown event on your inner TabControl and set e.Handled = true to prevent it from handling key events. You can then find the parent TabControl (perhaps recursively through ((TabControl)sender).Parent ) and change its SelectedIndex programmatically.
Wrapping that up in a custom control would keep it reasonably clean.
作为按照此处的建议创建自定义控件的替代方法,您可以创建一个“附加行为”来封装它
: XAML 像这样:
As an alternative to creating a Custom Control as suggested here, you could create an "Attached behaviour" to encapsulate this:
Use in XAML like this: