1
votes

i must confess i couldn't find a better title.

I have a Control which contains in the lower part a TextBox. This control has a ContextMenu and of course the TextBox has its normal text editing ContextMenu. When i right click the outer control its ContextMenu opens, which is what i want. If i right click the textbox it gets the focus and opens it text editing context menu. But when i right click the textbox and it doesn't has the keyboard focus i don't want the focus set and don't want to open the textbox contextmenu, instead it should open the outer control contextmenu.

The only thing i managed was to ignore the contextmenu of the textbox, when right clicking the textbox. (for testing i created a control derived from TextBox)

protected override void OnMouseDown(MouseButtonEventArgs aArgs)
    {
        if(aArgs.ChangedButton == MouseButton.Right)
        {
              return;
        }

        base.OnMouseDown(aArgs);
    }

Even doing this, the textboxes OnContextMenuOpening gets fired but no matter if handled is true or false, no context menu at all opens.

I hope someone has an idea.

1
Does the same thing happen if you use PreviewMouseDown instead of MouseDown? - Rachel
@Rachel hm, haven't tried it, but i would say no, because i don't handle the mouse event. I hoped, that returning from this mouse handling would pass the event to the next control underneath it. But i will give it a try. - dowhilefor

1 Answers

0
votes

The solution that works for me is so simple, i wonder why i didn't thought about it before. When the control is loaded or the element loses the focus, i just store the elements ContextMenu in a temporary variable and null the actual ContextMenu, if the element gets the Focus, i restore it from the temporary stored ContextMenu. This works suprisingly well, and can be nicely wrapped into an attached behavior.