Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force a ContextMenu to close (WPF project)?

I have a ListBox with items, and have assigned a ContextMenu to it with three menu items. Everything is working fine except that one of the menu items launches a lengthy operation. I would like to close the ContextMenu from the handler, and maybe display an hour-glass cursor or something.

Can that be done? Or, should I be using a Popup instead? If so, how do I use a Popup in lieu of a ContextMenu? My assumption is I would have to manage it completely - placement and lifetime.

Thanks!

like image 422
GTAE86 Avatar asked May 12 '10 16:05

GTAE86


People also ask

How to use ContextMenu in WPF?

A ContextMenu is attached to a specific control. The ContextMenu element enables you to present users with a list of items that specify commands or options that are associated with a particular control, for example, a Button. Users right-click the control to make the menu appear.

How do I close a page in WPF?

window. ShowDialog(); Close method of the Window class is used to close a window. The following code snippet calls the Close method to close a window.

How do I change a WPF window to a page?

Answers. You can either just host the Page in a Frame. Alternatively, just make a new Window, and put the same xaml content in the Window class. Migrate any code in the code behind to your new Window class.


1 Answers

What you want is:

myContextMenu.IsOpen = false;

Be sure to call this before your lengthy operation occurs. Depending on your operation, you may want to consider making it asynchronous by performing the operation on another thread - that way, you won't halt the application thread.

like image 106
AlishahNovin Avatar answered Oct 01 '22 13:10

AlishahNovin