WPF ContextMenu Closed Event,如何区分关闭方法

WPF ContextMenu Closed Event, how to differentiate closing method

在我的 Canvas 中,我有一个 ContextMenu,其预期功能是当您 select 菜单中的一个选项或单击菜单外部时关闭。但是,我希望我的程序根据使用哪些方法关闭 ContextMenu 而做出不同的响应。据我所知,在两种情况下都会触发 ContextMenu 上的 Closed 事件。有什么办法可以区分吗?

能否详细解释一下问题? 我认为很明显你必须设想。 1:单击您创建的 ContexMenu 选项,这将调用一个事件,然后单击它以外的地方,这将取消上下文菜单。顺便说一下,我根据对你问题的理解做了一个回答。如果我说错了请告诉我。

下面的代码只是一个演示代码,背后的逻辑很重要。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    // Windows Load event
    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        // Very Important Because by default it's null
        this.ContextMenu = new ContextMenu();

        // Making 3 sample Menu Item for ContextMenu
        MenuItem firstMenuItem = new MenuItem()
        {
            Header = "FirsMenu"
        };

        //1st of three way to give event to Controls. 
        // Giving click Event to firstMenuItem to seprate it's click behavior from Other Menu Items
        firstMenuItem.Click += (s, e) =>
        {
            // if Tag be Null on context Menu closing it's get that non of item selected so the click must be out side or lost focused
            this.ContextMenu.Tag = 1;
            MessageBox.Show("First Menu Clicked");
        };

        MenuItem secondMenuItem = new MenuItem()
        {
            Header = "SecondMenu"
        };

        //2nd of three way to give event to Controls. 
        // Giving click Event to secondMenuItem to seprate it's click behavior from Other Menu Items
        secondMenuItem.Click += delegate
        {
            // if Tag be Null on context Menu closing it's get that non of item selected so the click must be out side or lost focused
            this.ContextMenu.Tag = 1;
            MessageBox.Show("Second Menu Clicked");
        };

        MenuItem thirdMenuItem = new MenuItem()
        {
            Header = "ThirdMenu"
        };

        //3rd of three way to give event to Controls. 
        // Giving click Event to thirdMenuItem to seprate it's click behavior from Other Menu Items
        thirdMenuItem.Click += ThirdMenuOnClick;

        this.ContextMenu.Items.Add(firstMenuItem);
        this.ContextMenu.Items.Add(secondMenuItem);
        this.ContextMenu.Items.Add(thirdMenuItem);

        this.ContextMenu.Closed += ContextMenuOnClosed;
    }

    private void ThirdMenuOnClick(object sender, RoutedEventArgs e)
    {
        // if Tag be Null on context Menu closing it's get that non of item selected so the click must be out side or lost focused
        this.ContextMenu.Tag = 1;
        MessageBox.Show("Third Menu Clicked");
    }

    // Event for opening contextmenu on right mouse button click 
    private void MainWindow_OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.ContextMenu.IsOpen = true;
    }

    private void ContextMenuOnClosed(object sender, RoutedEventArgs e)
    {
        // if null means click must be out side or lost focused
        if (((ContextMenu)sender).Tag == null)
        {
            MessageBox.Show("You Clicked OutSide");
        }

        // very Importnt code , because it will reset the context menu tag logically
        ((ContextMenu)sender).Tag = null;
    }

}

祝你一切顺利,Heydar。