相关疑难解决方法(0)

如何使用WPF中的Alt键切换主菜单可见性?

我希望我的WPF应用程序中的主菜单的行为类似于IE8中的主菜单:

  • 应用程序启动时它不可见
  • 按下并释放Alt使其可见
  • 再次按下并释放Alt使其再次不可见
  • 重复直到无聊

我怎样才能做到这一点?它必须是代码吗?

添加以回应提交的答案,因为我仍然遇到麻烦:

我的Shell代码隐藏现在看起来像这样:

public partial class Shell : Window
{
    public static readonly DependencyProperty IsMainMenuVisibleProperty;

    static Shell()
    {
        FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
        metadata.DefaultValue = false;

        IsMainMenuVisibleProperty = DependencyProperty.Register(
            "IsMainMenuVisible", typeof(bool), typeof(Shell), metadata);
    }

    public Shell()
    {
        InitializeComponent();

        this.PreviewKeyUp += new KeyEventHandler(Shell_PreviewKeyUp);
    }

    void Shell_PreviewKeyUp(object sender, KeyEventArgs e)
    {
        if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
        {
            if (IsMainMenuVisible == true)
                IsMainMenuVisible = false;
            else
                IsMainMenuVisible = true;
        }
    }

    public bool IsMainMenuVisible
    { …
Run Code Online (Sandbox Code Playgroud)

wpf visibility keyboard-shortcuts menu toggle

8
推荐指数
1
解决办法
5609
查看次数

标签 统计

keyboard-shortcuts ×1

menu ×1

toggle ×1

visibility ×1

wpf ×1