RichEditControl.PopupMenuShowing Event
Occurs before a popup menu is created for the control’s document every time a context menu is being invoked.
Namespace: DevExpress.Xpf.RichEdit
Assembly: DevExpress.Xpf.RichEdit.v19.1.dll
Declaration
Event Data
The PopupMenuShowing event's data class is PopupMenuShowingEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
Customizations | Provides access to a collection of customizations of the popup menu. |
Menu | Gets or sets the popup menu displayed in the RichEditControl. |
MenuType | Gets a visual element for which the popup menu is invoked. |
Remarks
Handle the PopupMenuShowing event to modify items in the context menu. The current context menu can be accessed via the PopupMenuShowingEventArgs.Menu property.
Example
This example demonstrates how to customize the RichEditControl’s context menu - remove the existing menu items and add new items.
Handle the RichEditControl.PopupMenuShowing
event. Use the PopupMenuShowingEventArgs.MenuType property to determine the visual element for which the popup menu is invoked.
To remove a menu item, create the RemoveRichEditCommandAction object, set its ID to the ID of the command to remove and add that object to the PopupMenuShowingEventArgs.Customizations collection.
To add a new menu item, create a new BarButtonItem and add it to the PopupMenuShowingEventArgs.Customizations collection.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-customize-the-popup-menu-of-the-wpf-richedit-control-t537265.
private void RichEditPopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
if ((e.MenuType & RichEditMenuType.TableCell) != 0) {
// Remove the unnecessary buit-in items by their indices.
for (int i = e.Menu.Items.Count - 1; i >= 14; i--) {
e.Menu.Items.RemoveAt(i);
}
// Remove the "Cut" and "Copy" menu items.
e.Customizations.Add(new RemoveRichEditCommandAction() { Id = RichEditCommandId.CutSelection});
e.Customizations.Add(new RemoveRichEditCommandAction() { Id = RichEditCommandId.CopySelection });
// Create a menu item for the RichEdit command, which invokes the Insert Picture dialog.
e.Customizations.Add(new BarButtonItem() {
Command = RichEditUICommand.InsertPicture,
Content = "Insert Picture",
CommandParameter = richEdit
});
// Create a custom menu item and handle its click event.
BarButtonItem menuItem = new BarButtonItem();
menuItem.Name = "customHighlightItem";
menuItem.Content = "Highlight Selection";
menuItem.ItemClick += new ItemClickEventHandler(customHighlightItem_ItemClick);
e.Customizations.Add(menuItem);
}
}
private void customHighlightItem_ItemClick(object sender, ItemClickEventArgs e) {
CharacterProperties charProps = richEdit.Document.BeginUpdateCharacters(richEdit.Document.Selection);
charProps.BackColor = System.Drawing.Color.Yellow;
richEdit.Document.EndUpdateCharacters(charProps);
}
Related GitHub Examples
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.