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

时间:2016-03-25 16:05:45

标签: c# wpf xaml

我尝试了各种解决方案,例如here找到的解决方案,其中包含以下接受的答案:

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

然而,在实现上述解决方案之后,我仍然未能成功收听ContextMenuClosing事件,无论它如何关闭(进行选择,单击父按钮,单击菜单或按钮的任何位置)。以下是我的代码,我做错了什么?

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

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;
        }
    }
}

1 个答案:

答案 0 :(得分:3)

由于我public class myClassB{ 69) public myClassB(RandomAccessFile f, int k) throws IOException, FileNotFoundException{ 71) file = f; 72) file.seek(0); 73) byte[] url = new byte[256]; 74) file.read(url); 75) long rootIndex = file.readLong(); //This line 76) file.seek(rootIndex); ... } } 代码打开它时,ContextMenuClosing未知的原因似乎没有被触发。如果您通过右键单击按钮事件打开Click,则会按预期触发事件。作为一种解决方法,您可以使用似乎在两种情况下调用的ContextMenu.Closed事件

ContextMenu

并在您的代码中

<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>

唯一的区别似乎是你无法阻止private void myMenuButton_ContextMenu_Closed(object sender, RoutedEventArgs e) { Debug.WriteLine("myMenuButton_ContextMenu_Closed"); } 关闭,因为你可以在ContextMenu事件中