开发者

Open next ContextMenu in VisualTree when current Element has no focus

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

I have a Control which contains i开发者_运维百科n 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜