使 WPF TabControl 忽略 Ctrl+Tab

发布于 2024-07-19 06:44:49 字数 594 浏览 6 评论 0原文

我正在编写一个使用“选项卡式浏览”比喻的应用程序,其中 TabControl 为窗口的完整大小,以及选项卡内的其他内容。 有时,这些选项卡本身会包含其他 TabControl。

(选项卡内的选项卡可能会令人困惑,因此我将重新设置内部 TabControl 的样式,使其看起来不像 TabControl。我可能会将其样式设置为使用顶部的 ToggleButtons 而不是选项卡。)

我希望此 UI行为就像您期望选项卡式浏览隐喻一样工作: Ctrl+Tab 应始终在外部 TabControl(看起来像 TabControl 的那个)上切换选项卡,即使键盘焦点位于内部 TabControl 内部(它看起来不像 TabControl,因此不应期望响应 Ctrl+Tab)。 但是,当然,内部 TabControl 首先获取按键事件并自行处理。

阻止内部 TabControl 响应 Ctrl+TabCtrl+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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

碍人泪离人颜 2024-07-26 06:44:49

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.

傲影 2024-07-26 06:44:49

您可以处理内部 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.

站稳脚跟 2024-07-26 06:44:49

作为按照此处的建议创建自定义控件的替代方法,您可以创建一个“附加行为”来封装它

namespace WpfApplication1
{
  using System.Windows;
  using System.Windows.Input;

  public static class IgnoreCtrlTabBehaviour
  {
    //Setter for use in XAML: this "enables" this behaviour
    public static void SetEnabled(DependencyObject depObj, bool value)
    {
      depObj.SetValue(EnabledProperty, value);
    }

    public static readonly DependencyProperty EnabledProperty =
        DependencyProperty.RegisterAttached("Enabled", typeof(bool), 
        typeof(IgnoreCtrlTabBehaviour), 
        new FrameworkPropertyMetadata(false, OnEnabledSet));

    static void OnEnabledSet(DependencyObject depObj, DependencyPropertyChangedEventArgs args)
    {
      var uiElement = depObj as UIElement;
      uiElement.PreviewKeyDown += 
        (object _, System.Windows.Input.KeyEventArgs e) => 
        {
          if (e.Key == Key.Tab && 
              (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
          {
            e.Handled = true;
          }
        };
      }
    }
  }

: XAML 像这样:

<Window x:Class="WpfApplication1.MainWindow"
    ...
    xmlns:local="clr-namespace:WpfApplication1"

...
<TabControl local:IgnoreCtrlTabBehaviour.Enabled="True">
  <TabItem Header="tab1">
...

As an alternative to creating a Custom Control as suggested here, you could create an "Attached behaviour" to encapsulate this:

namespace WpfApplication1
{
  using System.Windows;
  using System.Windows.Input;

  public static class IgnoreCtrlTabBehaviour
  {
    //Setter for use in XAML: this "enables" this behaviour
    public static void SetEnabled(DependencyObject depObj, bool value)
    {
      depObj.SetValue(EnabledProperty, value);
    }

    public static readonly DependencyProperty EnabledProperty =
        DependencyProperty.RegisterAttached("Enabled", typeof(bool), 
        typeof(IgnoreCtrlTabBehaviour), 
        new FrameworkPropertyMetadata(false, OnEnabledSet));

    static void OnEnabledSet(DependencyObject depObj, DependencyPropertyChangedEventArgs args)
    {
      var uiElement = depObj as UIElement;
      uiElement.PreviewKeyDown += 
        (object _, System.Windows.Input.KeyEventArgs e) => 
        {
          if (e.Key == Key.Tab && 
              (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
          {
            e.Handled = true;
          }
        };
      }
    }
  }

Use in XAML like this:

<Window x:Class="WpfApplication1.MainWindow"
    ...
    xmlns:local="clr-namespace:WpfApplication1"

...
<TabControl local:IgnoreCtrlTabBehaviour.Enabled="True">
  <TabItem Header="tab1">
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文