Page 1 of 1

ContextMenu Close event problem

Posted: 18 Oct 2016, 09:33
by decai
Hi all,

We use ContextMenu in a Canvas in our project, when ContextMenu closed we want do something. How to determie the ContextMenu is closed by click out of the ContextMenu or clicked one item in the ContextMenu?

The ContextMenu Closing event happened before the item click event.

Thanks.

Re: ContextMenu Close event problem

Posted: 19 Oct 2016, 03:09
by sfernandez
Hi,

I verified the order of events in WPF and we are not raising them in the correct order.
If the user clicks a MenuItem the order should be:

1. MenuItem.Click
2. ContextMenu.Closed

We will fix it for the next release, sorry for the inconvenience.

Meanwhile, I don't know if it is possible in your situation, but maybe you can call VisualTreeHelper::HitTest() to decide if user clicked inside or outside the ContextMenu.

Re: ContextMenu Close event problem

Posted: 19 Oct 2016, 07:18
by decai
Hi,

Hittest is not work, but we found another way to fix it.

Thanks.

Re: ContextMenu Close event problem

Posted: 19 Oct 2016, 12:11
by sfernandez
Could you explain why HitTest failed?
And what was your final solution? It can help others in the same situation.

Thanks a lot for your feedback.

Re: ContextMenu Close event problem

Posted: 20 Oct 2016, 14:10
by decai
Hi,

Hittest always return false when closed the menu.

The solution we used is just like Hittest. We record the rect of contextmenu when it loaded, then test the mouse position if it in the rect when contextmenu closed.

Re: ContextMenu Close event problem

Posted: 21 Oct 2016, 11:08
by sfernandez
Hittest always return false when closed the menu.
Ok, that makes sense, as MenuItems are being closed and removed from the tree until ContextMenu parent is reached and its Closed event is raised, so HitTest cannot find the original MenuItem then.

Thanks ;)