Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按ctrl+打开关联菜单;菜单_C#_Wpf_Contextmenu - Fatal编程技术网

C# 按ctrl+打开关联菜单;菜单

C# 按ctrl+打开关联菜单;菜单,c#,wpf,contextmenu,C#,Wpf,Contextmenu,我想通过按ctrl+menu以独占方式打开以下上下文菜单: <Window.ContextMenu> <ContextMenu> <MenuItem Name="item0" Header="mode_0"/> </ContextMenu> </Window.ContextMenu> 检查pressedKey事件后,但松开菜单按钮后,contextMenu立即关闭。 有谁知道更好的。。以及工作方式?我不

我想通过按ctrl+menu以独占方式打开以下上下文菜单:

<Window.ContextMenu>
    <ContextMenu>
        <MenuItem Name="item0" Header="mode_0"/>
    </ContextMenu>
</Window.ContextMenu>
检查pressedKey事件后,但松开菜单按钮后,contextMenu立即关闭。
有谁知道更好的。。以及工作方式?

我不确定我是否正确理解您在寻找什么。
您可能应该这样做:

    public MainWindow()
    {
      InitializeComponent();
      this.PreviewKeyUp += MainWindow_PreviewKeyUp;

    }

    void MainWindow_PreviewKeyUp(object sender, KeyEventArgs e)
    {
      if (e.Key == Key.Apps)
      {
        e.Handled = true;
      }
    }
定义命令

 OpenMenu = new RoutedUICommand("OpenMenu ", "OpenMenu ", typeof(Commands), new   InputGestureCollection { 
                new KeyGesture(Key.O, ModifierKeys.Control, "Ctrl+O") });
在你的窗口使用它

    <Window.CommandBindings>
        <CommandBinding Command="local:Commands.OpenMenu "
                        Executed="OpenMenuExecuted" />
   </Window.CommandBindings>

    private void OpenMenuExecuted(object sender, ExecutedRoutedEventArgs e)
    {
           this.ContextMenu.IsOpen = true;          
    }

private void OpenMenuExecuted(对象发送方,ExecutedRoutedEventArgs e)
{
this.ContextMenu.IsOpen=true;
}

注意:如果您不希望右键单击按钮打开窗口
ContextMenu
,可以通过附加
ContextMenu打开事件处理程序并设置
e.Handled=true来阻止它

我认为您必须禁用窗口中的菜单键,否则操作系统将尝试解决此问题

诸如此类:

    public MainWindow()
    {
      InitializeComponent();
      this.PreviewKeyUp += MainWindow_PreviewKeyUp;

    }

    void MainWindow_PreviewKeyUp(object sender, KeyEventArgs e)
    {
      if (e.Key == Key.Apps)
      {
        e.Handled = true;
      }
    }

然后以编程方式打开您自己的上下文菜单。

Argh,菜单按钮会在keyUp事件中触发。。。我一直在听按键事件。这就是为什么按下键打开菜单,然后松开菜单按钮关闭菜单。

什么是“应用程序按钮”?你是说Windows的钥匙吗?哦,这个活动。钥匙叫做应用程序。。。我的意思是:在我的时代,那被称为菜单键。这很有意义,因为它用于调用上下文菜单。我不知道为什么。我试着在我的eventlistener中使用e.handled,但没有成功