WPF:禁用 tabcontrol 上的箭头键 [英] WPF: Disable arrow-keys on tabcontrol

查看:55
本文介绍了WPF:禁用 tabcontrol 上的箭头键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用 WPF TabControl 以便在程序的不同区域/功能之间切换.

I'm using the WPF TabControl in my application in order to switch between different areas/functions of the program.

不过有一件事让我很恼火.我隐藏了选项卡,因此我可以控制选定的选项卡,而不是用户.但是,用户仍然可以使用箭头键在选项卡之间切换.

One thing annoys me though. I have hidden the tabs so I can control the selectedtab, instead of the user. The user however, can still switch between tabs using the arrow-keys.

我已尝试使用 KeyboardNavigation 属性,但无法正常工作.

I have tried using the KeyboardNavigation-attribute, but I can't get this working.

可以禁用吗?

推荐答案

您可以为这个事件挂上 TabControl.PreviewKeyDown 事件.检查是左箭头还是右箭头,然后说您已经处理过了.

You can hook on to the TabControl.PreviewKeyDown event for this one. Check to see if it's the left or right arrow and say that you've handled it.

    private void TabControl_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Left || e.Key == Key.Right)
            e.Handled = true;
    }

如果您使用的是纯视图模型应用程序,则可以将上述内容作为附加属性应用.

if you're using a pure view model application you could apply the above as an attached property.

XAMl 使用下面的附加属性.

XAMl to use the below attached property.

<TabControl local:TabControlAttached.IsLeftRightDisabled="True">
    <TabItem Header="test"/>
    <TabItem Header="test"/>
</TabControl>

TabControlAttached.cs

TabControlAttached.cs

public class TabControlAttached : DependencyObject
{
    public static bool GetIsLeftRightDisabled(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsLeftRightDisabledProperty);
    }

    public static void SetIsLeftRightDisabled(DependencyObject obj, bool value)
    {
        obj.SetValue(IsLeftRightDisabledProperty, value);
    }

    // Using a DependencyProperty as the backing store for IsLeftRightDisabled.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsLeftRightDisabledProperty =
        DependencyProperty.RegisterAttached("IsLeftRightDisabled", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(false, new PropertyChangedCallback((s, e) =>
        {
            // get a reference to the tab control.
            TabControl targetTabControl = s as TabControl;
            if (targetTabControl != null)
            {
                if ((bool)e.NewValue)
                {
                    // Need some events from it.
                    targetTabControl.PreviewKeyDown += new KeyEventHandler(targetTabControl_PreviewKeyDown);
                    targetTabControl.Unloaded += new RoutedEventHandler(targetTabControl_Unloaded);
                }
                else if ((bool)e.OldValue)
                {
                    targetTabControl.PreviewKeyDown -= new KeyEventHandler(targetTabControl_PreviewKeyDown);
                    targetTabControl.Unloaded -= new RoutedEventHandler(targetTabControl_Unloaded);
                }
            }
        })));

    static void targetTabControl_Unloaded(object sender, RoutedEventArgs e)
    {

        TabControl targetTabControl = sender as TabControl;
        targetTabControl.PreviewKeyDown -= new KeyEventHandler(targetTabControl_PreviewKeyDown);
        targetTabControl.Unloaded -= new RoutedEventHandler(targetTabControl_Unloaded);
    }

    static void targetTabControl_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Left || e.Key == Key.Right)
            e.Handled = true;
    }
}

这篇关于WPF:禁用 tabcontrol 上的箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆