How can I find out when the default context menu for a text field will open or close?

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

+7
source share
2 answers

When the text field context menu opens, the text field loses keyboard focus but retains logical focus. In other words, the LostKeyboardFocus event will fire, but its LostFocus event will not. The IsFocused text box property remains true when the context menu is open. Based on your description of the problem, you should be able to rely on logical focus to determine when to end the โ€œedit modeโ€ in your control.

+6
source

By default, the TextBox still reports true for IsFocused , while the default context menu is open, but reports false for IsKeyboardFocused . That is, LostFocus does not rise on the TextBox when the context menu is open, but will be raised if the context menu is closed by selecting another control. This is similar to what you are looking for.

You can show this default behavior with a small test program:

 <Grid> <StackPanel> <TextBox Text="Some text one" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus"/> <TextBox Text="Some text two"/> </StackPanel> </Grid> 

and code:

  private void TextBox_GotFocus(object sender, RoutedEventArgs e) { Debug.WriteLine("GotFocus"); } private void TextBox_LostFocus(object sender, RoutedEventArgs e) { Debug.WriteLine("LostFocus"); } 

If you do not get this default behavior in the context of your larger application, then there may be a problem with focus focus.

+1
source

All Articles