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.
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);
}
<dx:DXWindow.Resources>
<dxre:RichEditUICommand x:Key="commands" />
</dx:DXWindow.Resources>
<dxb:BarManager x:Name="barManager1" ToolbarGlyphSize="Small">
<dxb:BarManager.Items>
<dxb:BarButtonItem
x:Name="customSymbol"
Command="{Binding InsertSymbol, Mode=OneTime, Source={StaticResource commands}}"
Content="Insert Symbol" />
<dxb:BarButtonItem
x:Name="customInsertTable"
Command="{Binding InsertTable, Mode=OneTime, Source={StaticResource commands}}"
Content="Insert Table" />
<dxb:BarButtonItem
x:Name="customShowTableGrid"
Command="{Binding TableToggleShowGridlines, Mode=OneTime, Source={StaticResource commands}}"
Content="Show Gridlines" />
<dxb:BarButtonItem
x:Name="customSetAllBorders"
Command="{Binding TableToggleAllBorders, Mode=OneTime, Source={StaticResource commands}}"
Content="All Borders" />
</dxb:BarManager.Items>
<Grid>
<dxre:RichEditControl x:Name="richEdit" CommandBarStyle="Empty" BarManager="{Binding ElementName=barManager1, Mode=OneTime}" PopupMenuShowing="RichEditPopupMenuShowing">
<dxre:RichEditControl.MenuCustomizations>
<dxre:RichEditMenuCustomization>
<dxre:RemoveRichEditCommandAction Id="{x:Static dxrecore:RichEditCommandId.CutSelection}"/>
<dxb:BarButtonItemLink dxb:BarItemLinkActionBase.ItemLinkIndex="0">
<dxb:BarButtonItemLink.BarItemName>
customSymbol
</dxb:BarButtonItemLink.BarItemName>
</dxb:BarButtonItemLink>
<dxb:BarButtonItemLink dxb:BarItemLinkActionBase.ItemLinkIndex="1">
<dxb:BarButtonItemLink.BarItemName>
customInsertTable
</dxb:BarButtonItemLink.BarItemName>
</dxb:BarButtonItemLink>
<dxb:BarItemLinkSeparator dxb:BarItemLinkActionBase.ItemLinkIndex="2"></dxb:BarItemLinkSeparator>
</dxre:RichEditMenuCustomization>
<dxre:RichEditMenuCustomization MenuType="TableCell">
<dxb:BarItemLinkSeparator></dxb:BarItemLinkSeparator>
<dxb:BarButtonItemLink>
<dxb:BarButtonItemLink.BarItemName>
customShowTableGrid
</dxb:BarButtonItemLink.BarItemName>
</dxb:BarButtonItemLink>
<dxb:BarButtonItemLink>
<dxb:BarButtonItemLink.BarItemName>
customSetAllBorders
</dxb:BarButtonItemLink.BarItemName>
</dxb:BarButtonItemLink>
</dxre:RichEditMenuCustomization>
</dxre:RichEditControl.MenuCustomizations>
</dxre:RichEditControl>
</Grid>
</dxb:BarManager>
Private Sub RichEditPopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
If (e.MenuType And RichEditMenuType.TableCell) <> 0 Then
' Remove the unnecessary buit-in items by their indices.
For i As Integer = e.Menu.Items.Count - 1 To 14 Step -1
e.Menu.Items.RemoveAt(i)
Next i
' Remove the "Cut" and "Copy" menu items.
e.Customizations.Add(New RemoveRichEditCommandAction() With {.Id = RichEditCommandId.CutSelection})
e.Customizations.Add(New RemoveRichEditCommandAction() With {.Id = RichEditCommandId.CopySelection})
' Create a menu item for the RichEdit command, which invokes the Insert Picture dialog.
e.Customizations.Add(New BarButtonItem() With { _
.Command = RichEditUICommand.InsertPicture, _
.Content = "Insert Picture", _
.CommandParameter = richEdit _
})
' Create a custom menu item and handle its click event.
Dim menuItem As New BarButtonItem()
menuItem.Name = "customHighlightItem"
menuItem.Content = "Highlight Selection"
AddHandler menuItem.ItemClick, AddressOf customHighlightItem_ItemClick
e.Customizations.Add(menuItem)
End If
End Sub
Private Sub customHighlightItem_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
Dim charProps As CharacterProperties = richEdit.Document.BeginUpdateCharacters(richEdit.Document.Selection)
charProps.BackColor = System.Drawing.Color.Yellow
richEdit.Document.EndUpdateCharacters(charProps)
End Sub