Back to Devexpress

RichEditControl.PopupMenuShowing Event

wpf-devexpress-dot-xpf-dot-richedit-dot-richeditcontrol-61aa0c85.md

latest8.6 KB
Original Source

RichEditControl.PopupMenuShowing Event

Allows you to customize the Rich Text Editor’s context menus.

Namespace : DevExpress.Xpf.RichEdit

Assembly : DevExpress.Xpf.RichEdit.v25.2.dll

NuGet Package : DevExpress.Wpf.RichEdit

Declaration

csharp
public event PopupMenuShowingEventHandler PopupMenuShowing
vb
Public Event PopupMenuShowing As PopupMenuShowingEventHandler

Event Data

The PopupMenuShowing event's data class is PopupMenuShowingEventArgs. The following properties provide information specific to this event:

PropertyDescription
CustomizationsProvides access to the collection of context menu customization actions.
MenuGets or sets the context menu for which the event is raised.
MenuTypeIndicates a document element for which the context menu is invoked.

Remarks

Use the PopupMenuShowingEventArgs.Customizations property to add or remove menu items. The PopupMenuShowingEventArgs.MenuType property allows you to determine for which element (document text, a header or footer, a picture, etc.) the menu is invoked. To obtain the context menu for which the event is raised, use the PopupMenuShowingEventArgs.Menu property.

The example below customizes the Text context menu as follows:

  • Creates new Highlight Selection and Insert Picture items
  • Removes the Increase Indent and Decrease Indent items

xaml
<dxre:RichEditControl Name="richTextEditor" 
                      PopupMenuShowing="richTextEditor_PopupMenuShowing">
csharp
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.Xpf.RichEdit;
using DevExpress.Xpf.Bars;
using DevExpress.Xpf.RichEdit.Menu;
using DevExpress.XtraRichEdit.Commands;
// ...

private void richTextEditor_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    // Check whether the event is raised for document text.
    if (e.MenuType == RichEditMenuType.Text) {
        // Create a menu item to insert a picture
        // and bind this item to the Rich Text Editor's command.
        e.Customizations.Add(new BarButtonItem() {
            Command = RichEditUICommand.InsertFloatingPicture,
            Content = "Insert Picture",
            CommandParameter = richTextEditor
        });

        // Create a custom menu item to highlight selected text.
        var menuItem = new BarButtonItem() {
            Name = "highlightSelectionItem",
            Content = "Highlight Selection"
        };
        menuItem.ItemClick += MenuItem_ItemClick; ;
        e.Customizations.Add(menuItem);
    }

    // Remove the "Increase Indent" item from the menu.
    e.Customizations.Add(new RemoveRichEditCommandAction() {
        Id = RichEditCommandId.IncreaseIndent
    });

    // Remove the "Decrease Indent" item from the menu.
    e.Customizations.Add(new RemoveRichEditCommandAction() {
        Id = RichEditCommandId.DecreaseIndent
    });
}

private void MenuItem_ItemClick(object sender, ItemClickEventArgs e) {
    var selectedRanges = richTextEditor.Document.Selections;
    foreach (var range in selectedRanges) {
        var charProps = richTextEditor.Document.BeginUpdateCharacters(range);
        charProps.BackColor = System.Drawing.Color.Yellow;
        richTextEditor.Document.EndUpdateCharacters(charProps);
    }
}
vb
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.Xpf.RichEdit
Imports DevExpress.Xpf.Bars
Imports DevExpress.Xpf.RichEdit.Menu
Imports DevExpress.XtraRichEdit.Commands
' ...

Private Sub richTextEditor_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
    ' Check whether the event is raised for document text.
    If e.MenuType = RichEditMenuType.Text Then
        ' Create a menu item to insert a picture
        ' and bind this item to the Rich Text Editor's command.
        e.Customizations.Add(New BarButtonItem() With {
            .Command = RichEditUICommand.InsertFloatingPicture,
            .Content = "Insert Picture",
            .CommandParameter = richTextEditor
        })

        ' Create a custom menu item to highlight selected text.
        Dim menuItem As New BarButtonItem() With {
            .Name = "highlightSelectionItem",
            .Content = "Highlight Selection"
        }
        menuItem.ItemClick += MenuItem_ItemClick
        e.Customizations.Add(menuItem)
    End If

    ' Remove the "Increase Indent" item from the menu.
    e.Customizations.Add(New RemoveRichEditCommandAction() With {
        .Id = RichEditCommandId.IncreaseIndent})

    ' Remove the "Decrease Indent" item from the menu.
    e.Customizations.Add(New RemoveRichEditCommandAction() With {
        .Id = RichEditCommandId.DecreaseIndent})
End Sub

Private Sub MenuItem_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
    Dim selectedRanges As SelectionCollection = richTextEditor.Document.Selections
    For Each range As DocumentRange In selectedRanges
        Dim charProps As CharacterProperties = richTextEditor.Document.BeginUpdateCharacters(range)
        charProps.BackColor = System.Drawing.Color.Yellow
        richTextEditor.Document.EndUpdateCharacters(charProps)
    Next range
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the PopupMenuShowing event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-rich-text-editor-customize-context-menu/CS/ContextMenuCustomization/MainWindow.xaml#L38

xml
<dxre:RichEditControl x:Name="richEdit" CommandBarStyle="Empty" BarManager="{Binding ElementName=barManager1, Mode=OneTime}" PopupMenuShowing="RichEditPopupMenuShowing">
    <dxre:RichEditControl.MenuCustomizations>

wpf-rich-text-editor-customize-context-menu/CS/ContextMenuCustomization/obj/Debug/net5.0-windows/MainWindow.g.cs#L157

csharp
#line 38 "..\..\..\MainWindow.xaml"
this.richEdit.PopupMenuShowing += new DevExpress.Xpf.RichEdit.PopupMenuShowingEventHandler(this.RichEditPopupMenuShowing);

wpf-rich-text-editor-customize-context-menu/VB/ContextMenuCustomization/obj.NetFX/Debug/MainWindow.g.vb#L156

vb
#ExternalSource("..\..\MainWindow.xaml",38)
AddHandler Me.richEdit.PopupMenuShowing, New DevExpress.Xpf.RichEdit.PopupMenuShowingEventHandler(AddressOf Me.RichEditPopupMenuShowing)

See Also

Menu

How to: Customize Context Menus for the Rich Text Editor

RichEditControl Class

RichEditControl Members

DevExpress.Xpf.RichEdit Namespace