Back to Devexpress

RichEditControl.MenuCustomizations Property

wpf-devexpress-dot-xpf-dot-richedit-dot-richeditcontrol-cac4465a.md

latest7.1 KB
Original Source

RichEditControl.MenuCustomizations Property

Allows you to customize the Rich Text Editor’s context menus. This is a dependency property.

Namespace : DevExpress.Xpf.RichEdit

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

NuGet Package : DevExpress.Wpf.RichEdit

Declaration

csharp
public ObservableCollection<RichEditMenuCustomization> MenuCustomizations { get; set; }
vb
Public Property MenuCustomizations As ObservableCollection(Of RichEditMenuCustomization)

Property Value

TypeDescription
ObservableCollection<RichEditMenuCustomization>

A collection of menu customization actions.

|

Remarks

Add the InsertAction, UpdateAction or RemoveAction customization actions to the RichEditControl.MenuCustomizations collection to create, modify, or remove items of the Rich Text Editor’s context menus. Use the RichEditMenuCustomization.MenuType property to specify the menu type you want to customize.

The example below demonstrates how to customize the Text context menu.

View Example

xaml
<dx:ThemedWindow x:Class="WpfRichEditorMenuCustomization.MainWindow" mc:Ignorable="d" Title="Rich Text Editor" 
                 Height="450" Width="800"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfRichEditorMenuCustomization"
    xmlns:dxre="http://schemas.devexpress.com/winfx/2008/xaml/richedit" 
    xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars">
    <Grid>
        <dxre:RichEditControl Name="richTextEditor">
            <dxre:RichEditControl.MenuCustomizations>
                <dxre:RichEditMenuCustomization MenuType="Text">
                    <!--Add a separator after the first three menu items.-->
                    <dxb:InsertAction Index="4">
                        <dxb:BarItemSeparator />
                    </dxb:InsertAction>
                    <!--Insert a custom menu item to highlight selected text.-->
                    <dxb:InsertAction Index="5">
                        <dxb:BarButtonItem Content="Highlight Selection" 
                                           ItemClick="HighlightSelection_ItemClick" />
                    </dxb:InsertAction>
                    <!--Add a new item to the end of the context menu
                    and bind this item to the Rich Text Editor's command.-->
                    <dxb:BarButtonItem Command="{Binding RelativeSource={RelativeSource Self}, Mode=OneWay, 
                        Path=(dxre:RichEditControl.RichEdit).CommandProvider.InsertFloatingPicture}" 
                        Content="Insert Picture" />
                    <!--Change the "New Comment" item's content.-->
                    <dxb:UpdateAction ElementName="{x:Static dxre:DefaultBarItemNames.PopupMenuItem_NewComment}"
                                      PropertyName="Content"
                                      Value="Add Comment"/>
                    <!--Remove the "Increase Indent" item from the menu.-->
                    <dxb:RemoveAction ElementName="{x:Static dxre:DefaultBarItemNames.PopupMenuItem_IncreaseIndent}"/>
                    <!--Remove the "Decrease Indent" item from the menu.-->
                    <dxb:RemoveAction ElementName="{x:Static dxre:DefaultBarItemNames.PopupMenuItem_DecreaseIndent}"/>
                </dxre:RichEditMenuCustomization>
            </dxre:RichEditControl.MenuCustomizations>
        </dxre:RichEditControl>
    </Grid>
</dx:ThemedWindow>

Handle the Highlight Selection item’s ItemClick event to highlight selected text in yellow.

csharp
using DevExpress.XtraRichEdit.API.Native;
// ...

private void HighlightSelection_ItemClick(object sender, DevExpress.Xpf.Bars.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
Private Sub HighlightSelection_ItemClick(ByVal sender As Object,
    ByVal e As DevExpress.Xpf.Bars.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 MenuCustomizations property.

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/WpfRichEditorMenuCustomization/MainWindow.xaml#L13

xml
<dxre:RichEditControl Name="richTextEditor">
    <dxre:RichEditControl.MenuCustomizations>
        <dxre:RichEditMenuCustomization MenuType="Text">

See Also

How to: Customize Context Menus for the Rich Text Editor

RichEditControl Class

RichEditControl Members

DevExpress.Xpf.RichEdit Namespace