简体   繁体   English

如何在WPF中订阅上下文菜单关闭事件?

[英]How do I subscribe to context menu closing event in WPF?

I have attempted various solutions, such as the one found here , which has the accepted answer of the following: 我尝试了各种解决方案,例如在这里找到的解决方案,该解决方案具有以下公认的答案:

<Button ContextMenuClosing="ContextMenu_ContextMenuClosing">
    <Button.ContextMenu>
        <ContextMenu>
             <MenuItem Header="Go"/>
        </ContextMenu>
    </Button.ContextMenu>
 </Button>

However, after implementing the above solution, I have still been unsuccessful listening to the ContextMenuClosing event, no matter how it is closed (making a selection, clicking the parent button, clicking anywhere off the menu or button). 但是,实现上述解决方案后,无论如何关闭,我仍然无法成功监听ContextMenuClosing事件(进行选择,单击父按钮,单击菜单或按钮的任意位置)。 Below is my code, what am I doing wrong? 以下是我的代码,我在做什么错?

TestPage.xaml TestPage.xaml

<Page x:Class="MyProject.pages.TestPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:MIMOUI.pages"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="300"
    Title="TestPage">

    <Grid>
        <Button x:Name="myMenuButton" 
            Width="140" 
            Height="100" 
            Click="myMenuButton_Click"
            ContextMenuClosing="myMenuButton_ContextMenuClosing"
            Background="NavajoWhite" 
            Foreground="BurlyWood">

            <Button.ContextMenu>
                <ContextMenu x:Name="myMenuButton_ContextMenu" Width="250">
                    <MenuItem x:Name="myTaskMenuButton" Header="TASKS" />
                    <MenuItem x:Name="myTransactionButton" Header="TRANSACTION" />
                    <MenuItem x:Name="mySetupMenuButton" Header="SETUP" />
                </ContextMenu>
            </Button.ContextMenu>
        </Button>
    </Grid>
</Page>

TestPage.xaml.cs TestPage.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace MyProject.pages {
    public partial class TestPage : Page {
        public TestPage() {
            InitializeComponent();
        }

        public void myMenuButton_Click(object sender, RoutedEventArgs e) {
            (sender as Button).ContextMenu.IsOpen = true;
        }

        public void myMenuButton_ContextMenuClosing(object sender, RoutedEventArgs e) {
            Console.WriteLine("intercepted!!!!");
            e.Handled = true;
        }
    }
}

For reasons unknown to me ContextMenuClosing seems not to be triggered when it was opened by your Click code. 由于我不知道的原因,当您的Click代码打开ContextMenuClosing似乎没有触发它。 If you would open ContextMenu by right-click on your button event is triggered as expected. 如果您要通过右键单击打开ContextMenu ,则将按预期触发事件。 As a workaround you can use ContextMenu.Closed event which seems to be called in both cases 作为一种解决方法,您可以使用ContextMenu.Closed事件,这两种情况似乎都被调用

<Button x:Name="myMenuButton" Click="myMenuButton_Click" ...>
    <Button.ContextMenu>
        <ContextMenu ... Closed="myMenuButton_ContextMenu_Closed">
            <MenuItem x:Name="myTaskMenuButton" Header="TASKS" />
            <MenuItem x:Name="myTransactionButton" Header="TRANSACTION" />
            <MenuItem x:Name="mySetupMenuButton" Header="SETUP" />
        </ContextMenu>
    </Button.ContextMenu>
</Button>

and in your code 并在您的代码中

private void myMenuButton_ContextMenu_Closed(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("myMenuButton_ContextMenu_Closed");
}

the only difference seems to be that you cannot stop ContextMenu from closing as you would be able to in ContextMenuClosing event 唯一的区别似乎是您无法像在ContextMenuClosing事件中一样能够阻止ContextMenu关闭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM