Context menu and MouseClick events

Aug 5, 2011 at 5:54 PM

Hello, I've got such issue:

Let's say that we have canvas, and some controls on this canvas (in my case I have UserControls of my own created type "Appointment"). Every Appointment is connected to his own context menu and is his only "rightClickElement". Also Canvas has its own context menu. At this moment when I right click on some Appointment, two context menus appear: for Appointment, and for canvas. The second context menu is not desirable, I don't want it to appear (I would like to use something like "e.Handled" for routed events). How to achieve this? And my second question is: how to show new Appointment on canvas and immediately show context menu for this new Appointment after I right click on this canvas (in this case I'm clicking on empty space on canvas, not occupied by some Appointment)?

Greetings from Poland

I would be grateful if you help me.

Coordinator
Aug 5, 2011 at 7:45 PM

 

Well there are several ways to achieve this. So here are some suggestions:

"At this moment when I right click on some Appointment, two context menus appear: for Appointment, and for canvas. The second context menu is not desirable, I don't want it to appear (I would like to use something like "e.Handled" for routed events). How to achieve this?"

 

 

Setting e.Handled as true from within the right click event of the Appointment control should work IMO. Otherwise you can use the GetClickedElements<T> function from within the IsOpening event of the PopupMenu itself to determine which control was clicked and disable unwanted menus from there using their IsOpeningCancelled property. You might as well just set e.Handled to true from there as well.

2. "How to show new Appointment on canvas and immediately show context menu for this new Appointment after I right click on this canvas (in this case I'm clicking on empty space on canvas, not occupied by some Appointment)?"

The Open and OpenNextTo method offer all the control you need on when an where you menu is placed in your application. In your case you might use either of them in the right click event of your canvas but this is just my 2 cents.

I've might have been a little too quick on reading your requirements so let me know if this doesn't help.

Regards

Ziad

Aug 8, 2011 at 9:22 AM

"Setting e.Handled as true from within the right click event of the Appointment control should work IMO."

It works if I don't use AddRightClickElements or AddTrigger methods, only right click events for Appointment and canvas.


"Otherwise you can use the GetClickedElements<T> function from within the IsOpening event of the PopupMenu itself to determine which control was clicked and disable unwanted menus from there using their IsOpeningCancelled property. You might as well just set e.Handled to true from there as well."

I can only see function named GetClickedElement<T>. How to use IsOpeningCancelled property? Can you show an example code? IsOpening is a boolean value, not event, I think you meant Opening. If so, I cannot use e.Handled in Opening event.


"The Open and OpenNextTo method offer all the control you need on when an where you menu is placed in your application. In your case you might use either of them in the right click event of your canvas but this is just my 2 cents."

Ok, these functions are helpful. But I've got another problem. How to make it show new Appointment and context menu every time I right click on canvas? At this moment I have to right click twice, the first time it closes the previous context menu, second click opens the new context menu. How to close previous menu and show the new one in just one click? I'm not using AddRightClickElements and AddTrigger methods, because getting rid of them solved my first issue.

Coordinator
Aug 24, 2011 at 7:12 PM
Edited Aug 24, 2011 at 7:16 PM

Sorry about that but there were a few confusing typos in my last reply. GetClickedElements<T> does not actually end with an 's' and as you figured out I used IsOpening(the property) instead of Opening(the event) and it is where e.Handled can be accessed.

As for IsOpeningCancelled it is meant to be used in the Opening event. Just set it to true if you want to cancel the event.

I'm not sure I got your point correctly for the double right click issue but my quick suggestion would be to use IsOpeningCancelled to cancel the opening of the first menu while using the Open method to programatically open the second menu from the Opening event itself. Yet you might do it the other way round by calling the Open method for the second menu from the Closing event for the first menu. However you might also need to add some checks in your code before doing this since you might not always want this to happen.

Jan 6, 2012 at 3:39 PM

I had the same problem and believe this is a bug.  PopupMenuBase::TriggerElement_MouseRightButtonUp() should set e.Handled to true, but it currently does not.  I added issue #7694 to address this.