How can I change the default context menu?

SyntaxEditor for WPF Forum

Posted 8 years ago by Craig - Varigence, Inc.
Version: 11.1.0541
Avatar
I want to add additional menu items to the default syntax editor context menu. I thought I might accomplish that by attaching an event handler to the ContextMenuOpening event and change the ContextMenu within the handler. However, I realize that's hacky and I'm also finding that the event never fires for me. Is there a way to modify the built-in context menu or do I need to provide my own?

Thanks,

-Craig

P.S. - I noticed that when I set a bp in my debugger and examine the SyntaxEditor, its ContextMenu property is null.

[Modified at 03/04/2011 11:55 AM]

Comments (8)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Craig,

Sorry we have to handle the ContextMenuOpening event in order to generate a menu and our tie unfortunately can only happen before it bubbles up higher.

Probably the best way for you to do a custom menu would be to either set the ContextMenu property, or inherit SyntaxEditor and override OnContextMenuOpening, then do your processing in that and don't call the base method. There isn't any way to tie into our menu right now. We basically just populate a normal ContextMenu with appropriate EditorCommands.


Actipro Software Support

Posted 8 years ago by Jonathan Reis
Avatar
Any update on this. It would be great to be able to extend your default context menu with a few custom items.
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Jonathan,

For the next maintenance release, we've added a virtual SyntaxEditor.CreateDefaultContextMenu method you can override to change items or add new ones before it gets displayed.


Actipro Software Support

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

I've been setting a custom ContextMenu via the SyntaxEditor's ContextMenu property for a long while now and overall, it works great. However, I've discovered an issue that I need help with.

If the SyntaxEditor's Document becomes readonly and then later becomes writeable, all the ContextMenu commands remain disabled. 

To repro this, you can use the below sample and the following steps:

  1. Type some characters into the syntax editor. Doesn't matter what.
  2. Check the 'Toggle Read-Only' checkbox
  3. Uncheck the 'Toggle Read-Only' checkbox
  4. Right click in the editor
Result:

You'll see that the context menu items are all disabled, despite some being enabled before step 2.

Question: Is there something I can change so that the context menus re-enable themselves appropriately? 

Thanks,

-Craig

 

Sample code:

MainWindow.xaml:

<Window 
    x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:SyntaxEditor="http://schemas.actiprosoftware.com/winfx/xaml/syntaxeditor"
    Title="MainWindow" 
    Height="350" 
    Width="525"
    >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <CheckBox
            x:Name="readonlyToggle"
            Content="Toggle Read-Only"
            Grid.Row="0"
            Click="ReadonlyToggle_OnClick"
            />
        <SyntaxEditor:SyntaxEditor
            x:Name="syntaxEditor"
            Grid.Row="1"
            IsDefaultContextMenuEnabled="False"
            >
            <SyntaxEditor:SyntaxEditor.ContextMenu>
                <ContextMenu>
                    <MenuItem
                        Header="Undo"
                        Command="SyntaxEditor:EditorCommands.Undo"
                        InputGestureText="Ctrl+Z"
                        />
                    <MenuItem
                        Header="Redo"
                        Command="SyntaxEditor:EditorCommands.Redo"
                        InputGestureText="Ctrl+Y"
                        />
                    <Separator 
                        />
                    <MenuItem
                        Header="Cu_t"
                        Command="SyntaxEditor:EditorCommands.CutToClipboard"
                        InputGestureText="Ctrl+X"
                        />
                    <MenuItem
                        Header="_Copy"
                        Command="SyntaxEditor:EditorCommands.CopyToClipboard"
                        InputGestureText="Ctrl+C"
                        />
                    <MenuItem
                        Header="_Paste"
                        Command="SyntaxEditor:EditorCommands.PasteFromClipboard"
                        InputGestureText="Ctrl+V"
                        />
                    <MenuItem
                        Header="_Delete"
                        Command="SyntaxEditor:EditorCommands.Delete"
                        />
                    <Separator
                        />
                    <MenuItem
                        Header="Select _All"
                        Command="SyntaxEditor:EditorCommands.SelectAll"
                        InputGestureText="Ctrl+A"
                        />
                </ContextMenu>
            </SyntaxEditor:SyntaxEditor.ContextMenu>
        </SyntaxEditor:SyntaxEditor>
    </Grid>
</Window>

 MainWindow.xaml.cs:

namespace WpfApplication4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ReadonlyToggle_OnClick(object sender, RoutedEventArgs e)
        {
            syntaxEditor.Document.IsReadOnly = readonlyToggle.IsChecked.GetValueOrDefault();
        }
    }
}
Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Craig,

Are you running the latest version?  Perhaps you don't have focus in the editor when the right-click is done?  Menu item commands operate on whatever has focus.  I believe in recent builds though we added something to focus on right-click.


Actipro Software Support

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

I'm testing this with WPF Studio 12.2.570. 

Some questions:

  1. Are you able to reproduce the issue with the sample code above? Your reply didn't explicitly state that.
  2. When building your context menu, are you setting the CommandParameter or CommandTarget properties on your menu items? If so, to what? (I'm wondering if my lack of a paramter and/or target is contributing to this)

In the meantime, I'll try authoring a behavior to focus the SyntaxEditor when a right click occurs.

-Craig

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

Yes I did test with your code and just looked up the build 571 release notes.  It looks like we added focus on right-click in that build.  So that's why it worked for me.


Actipro Software Support

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

That's great, thanks. Also, I've confirmed that if I use

private void SyntaxEditor_OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    syntaxEditor.ActiveView.Focus();
}

 with earlier builds, the ContextMenu works.

Thanks again,

-Craig

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

Add Comment

Please log in to a validated account to post comments.