Why are my custom context menu commands disabled when I right click first?

SyntaxEditor for WPF Forum

Posted 7 years ago by Craig - Varigence, Inc.
Version: 12.2.0570
Avatar

We have a SyntaxEditor that uses a custom context menu, like so:

<Window 
    x:Class="ContextMenuProblem.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:apeditor="clr-namespace:ActiproSoftware.Windows.Controls.SyntaxEditor;assembly=ActiproSoftware.SyntaxEditor.Wpf" 
    Title="MainWindow" 
    Height="350"
    Width="525"
    >
    <Grid>
        <apeditor:SyntaxEditor
            IsDefaultContextMenuEnabled="False"
            >
            <apeditor:SyntaxEditor.ContextMenu>
                <ContextMenu>
                    <MenuItem
                        Header="Cut"
                        Command="apeditor:EditorCommands.CutToClipboard"
                        InputGestureText="Ctrl+X"
                        />
                    <MenuItem
                        Header="Copy"
                        Command="apeditor:EditorCommands.CopyToClipboard"
                        InputGestureText="Ctrl+C"
                        />
                    <MenuItem
                        Header="Paste"
                        Command="apeditor:EditorCommands.PasteFromClipboard"
                        InputGestureText="Ctrl+V"
                        />
                    <MenuItem
                        Header="Delete"
                        Command="apeditor:EditorCommands.Delete"
                        />
                    <Separator
                        />
                    <MenuItem
                        Header="Select All"
                        Command="apeditor:EditorCommands.SelectAll"
                        InputGestureText="Ctrl+A"
                        />
                </ContextMenu>
            </apeditor:SyntaxEditor.ContextMenu>
        </apeditor:SyntaxEditor>
    </Grid>
</Window>

This works fine for us, except for one problem. When running this, if a user right clicks in the syntax editor (before left clicking - meaning no insertion pointer is present), all the commands are disabled. However ,when I use the built-in context menu and right click first, nearly all commands are enabled. 

What can I do to ensure that commands are enabled on the first right click? While I realize overriding CreateDefaultContextMenu is the recommended approach, we prefer to not extend controls whenever possible. I figure I could probably create a behavior that performs a left click on a right click but that seems hacky. I was wondering if there's a better alternative or a workaround.

Thanks,

-Craig

Comments (1)

Answer - Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Craig,

Routed commands work based on where focus is.  Since right clicking doesn't set focus, your focus is elsewhere and those commands are relating to whatever other control has the focus.  I believe we set the CommandTarget of the menu items to be the SyntaxEditor instance.  So perhaps if you add a binding like that, then it will fix the problem.

But now that you bring this up, we probably should just focus the control on a right-click.  We'll add this for the next maintenance release.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.