Example T537265
Visible to All Users

WPF Rich Text Editor - How to Customize Context Menus

This example demonstrates how to customize the Text context menu of the WPF Rich Text Editor.

Custom context menu for the Rich Text Editor

Add the required bar actions to the RichEditControl.MenuCustomizations collection to create, modify or remove menu items.

Documentation

Refer to the following help topic for implementation details: Customize Context Menus for the Rich Text Editor.

Files to Look At

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

WpfRichEditorMenuCustomization/MainWindow.xaml
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>
WpfRichEditorMenuCustomization/MainWindow.xaml.cs(vb)
C#
using DevExpress.XtraRichEdit.API.Native; using System; using System.Collections.Generic; using System.Linq; using System.Windows; namespace WpfRichEditorMenuCustomization { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : DevExpress.Xpf.Core.ThemedWindow { public MainWindow() { InitializeComponent(); richTextEditor.Document.Text = "The WPF Rich Text Editor allows you to integrate word processing " + "capabilities into your next WPF project. With its comprehensive text formatting options, " + "mail-merge, and a rich collection of end-user options you can deliver Microsoft Word-inspired " + "functionality with ease."; } 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); } } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.