How can I find out when the default context menu opens for a text field (before it opens) or after it is closed (not earlier, after)? The ContextMenuOpening and ContextMenuClosing events do not fire when I use the standard built-in menu. I guess I could just recreate the menu and populate it with standard commands, but that seems to have gone a bit too far.
The reason for this is because I have a template control in which the text field is replaced in the "Edit" mode. This control automatically leaves edit mode when the text field loses focus. The problem is that the context menu appears, the text field loses focus and, thus, it leaves the editing mode, and the context menu disappears instantly.
What I want to do is immediately before opening the context menu, set the flag for short circuiting the LostFocus event code in the text box. Then, after closing the context menu, I need to clear this flag, but I also need to determine whether the control, which now has focus, remains a text box, and if not, process the code as if it lost focus. (Alternatively, I could check the event before it closed, if I knew what control would have focus after it closed, and it would achieve the same effect.)
This is necessary to handle a specific case if someone displays a context menu (and therefore the text field technically no longer has focus), but then clicks on a different place in the user interface, which rejects the context menu, because then I need to find out that the text field is actually lost focus, and therefore control should exit edit mode. But if the user rejects the context menu by clicking back in the text box, I do not want this LostFocus event to fire.
Make sense?
M
UPDATE: Technically, I did not answer this question, although I marked it as such, since the respondents really helped me solve my problem. But as for the actual question here, it looks like the answer is "You can't."
The good news is that the default context menu in the text box is just three standard elements, itโs easy to duplicate it by adding it to resources somewhere ...
<ContextMenu x:Key="DefaultTextBoxContextMenu"> <MenuItem Command="ApplicationCommands.Cut" /> <MenuItem Command="ApplicationCommands.Copy" /> <MenuItem Command="ApplicationCommands.Paste" /> </ContextMenu>
... and attach it like this:
<TextBox x:Name="EditTextBox" ContextMenu="{StaticResource DefaultTextBoxContextMenu}" ContextMenuOpening="EditTextBox_ContextMenuOpening" ContextMenuClosing="EditTextBox_ContextMenuClosing" />
... then your events work as you expected. It is still strange if you ask me, but trivial work is around, so I wonโt complain.
M