【问题标题】:WPF ContextMenu using (Shift-Right-Click)WPF ContextMenu 使用(Shift-右键单击)
【发布时间】:2011-07-20 00:29:48
【问题描述】:

我对 WPF 中的“ContextMenu”有疑问。有没有办法只有在执行“Shift-Right-Click”时才会弹出上下文菜单? 我一直在到处寻找这个。 ContextMenu 似乎只能在“右键单击”时弹出。

大家有什么想法吗??

【问题讨论】:

    标签: wpf contextmenu shift right-click


    【解决方案1】:

    试试这个……你的 XAML 上下文菜单属性应该是这样的……

    <ElementToWhichContextMenuIsAttached ContextMenu="{StaticResource MyContextMenu}"
                                         ContextMenuOpening="MyContextMenuOpening"/>
    

    您的代码将如下所示。

        /// <summary>
        /// This will suppress the context menu if the shift key is not pressed
        /// </summary>
        private void MyContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            // Show context menu as handled if no key is down.
            e.Handled = !(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift));
        }
    

    【讨论】:

    • 甜它工作 tnx!顺便说一句,您不必使用 StaticResource,您可以将其设置为内容,但“ContextMenuOpening”将与您拥有的相同。我在基础设施控制中使用它,所以我必须这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 2012-07-20
    • 1970-01-01
    相关资源
    最近更新 更多