Using your own Context Menu

The control comes with a default Context Menu which serves the most common purpose. This context menu shows menu for Image Editor if the cursor is on an image, Hyperlink Editor if the cursor is on Hyperlink, Table Editor if the cursor is on Table etc. Also this context menu offers Table Manipulation Menu items like Add New Column, Add New Row, Delete Row, Delete Column etc.

Context menu image Context menu link Context menu table

Now, if you want to override this context menu with your own one, simply add a ContextMenu control to your editor using Visual Studio Property window and Add the menu items to your context menu according to your preference.

EditorContextMenuStrip property

Thats all you need to do in order to set your custom context menu. If you are thinking, how you could call the common functionalities from your context menu that are similar to Toolbox Item functions, you can find all the Toolbar button's related method from the ToolbarItemOverrider property as shown here:

ToolbarItemOverrider

How to control showing some of the menu item based on context ?

There is a event named ContextMenuShowing. Simply handle this event and from that event, you can set visible property of a particular menu item = TRUE / FALSE based on context.

The composite property StateQuery can help you to find out the current context.

C#

private void wpfHtmlEditor1_ContextMenuShowing(object sender, SpiceLogic.WpfHtmlEditor.EditorEventArgs.ContextMenuShowingEventArgs e)
{
    MenuItem contextMenuItemCopy = LogicalTreeHelper.FindLogicalNode(wpfHtmlEditor1.EditorContextMenuStrip, "contextMenuItem_delete") as MenuItem;
    if (contextMenuItemCopy != null)
        contextMenuItemCopy.IsEnabled = wpfHtmlEditor1.StateQuery.CanCopy();

    MenuItem contextMenuItemPaste = LogicalTreeHelper.FindLogicalNode(wpfHtmlEditor1.EditorContextMenuStrip, "contextMenuItem_paste") as MenuItem;
    if (contextMenuItemPaste != null)
        contextMenuItemPaste.IsEnabled = wpfHtmlEditor1.StateQuery.CanPaste();
    
}
VB

Private Sub wpfHtmlEditor1_ContextMenuShowing(sender As Object, e As SpiceLogic.WpfHtmlEditor.EditorEventArgs.ContextMenuShowingEventArgs)
	Dim contextMenuItemCopy As MenuItem = TryCast(LogicalTreeHelper.FindLogicalNode(wpfHtmlEditor1.EditorContextMenuStrip, "contextMenuItem_delete"), MenuItem)
	If contextMenuItemCopy IsNot Nothing Then
		contextMenuItemCopy.IsEnabled = wpfHtmlEditor1.StateQuery.CanCopy()
	End If

	Dim contextMenuItemPaste As MenuItem = TryCast(LogicalTreeHelper.FindLogicalNode(wpfHtmlEditor1.EditorContextMenuStrip, "contextMenuItem_paste"), MenuItem)
	If contextMenuItemPaste IsNot Nothing Then
		contextMenuItemPaste.IsEnabled = wpfHtmlEditor1.StateQuery.CanPaste()
	End If
End Sub

Dont want to start from scratch ? Rather, do you want to enhance the default context menu ? Yes, that is possible. We have provided the source code and the full implementation of the Default Context menu in the Download Package you received from our website. Please open that zip file and navigate to Samples folder. You will find complete implementation source code of the default context menu in a project 'Custom Context Menu'. You can simply change that based on your need.

If you do not want to show any context menu at all, you may set the "Options.DisableEditorRightClick" = True

DisableEditorRightClick property

Last updated on 29 May 2015, Friday, 8:55:42 AM
If you have any questions or concerns about this tutorial, Please feel free to share your comment.