Example E2030
Visible to All Users

WPF Data Grid - Use a RichTextBox to Display and Edit Grid Data

This example illustrates how to embed a standard RichTextBox control into GridControl cells. The CellDisplayTemplate and CellEditTemplate properties allow you to define different settings for display and edit modes.

image

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

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

Example Code

RichTextBoxEx.cs(vb)
C#
using System.IO; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; namespace GridApplication { public class RichTextBoxEx : RichTextBox { bool IsSettingText; public static readonly DependencyProperty RtfTextProperty = DependencyProperty.Register("RtfText", typeof(string), typeof(RichTextBoxEx), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnRtfTextChanged)); static void OnRtfTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((RichTextBoxEx)d).LoadText(); } public string RtfText { get { return GetValue(RtfTextProperty) as string; } set { SetValue(RtfTextProperty, value); } } protected override void OnTextChanged(TextChangedEventArgs e) { Binding binding = BindingOperations.GetBinding(this, RtfTextProperty); if (binding != null && binding.Mode != BindingMode.OneWay) { SaveText(); } base.OnTextChanged(e); } void LoadText() { if (IsSettingText) return; string rtfText = RtfText; Document.Blocks.Clear(); if (!string.IsNullOrEmpty(rtfText)) { TextRange tr = new TextRange(Document.ContentStart, Document.ContentEnd); using (MemoryStream rtfMemoryStream = new MemoryStream(Encoding.ASCII.GetBytes(rtfText))) { tr.Load(rtfMemoryStream, DataFormats.Rtf); } } } void SaveText() { TextRange tr = new TextRange(Document.ContentStart, Document.ContentEnd); string CurrentText = tr.Text; using (MemoryStream ms = new MemoryStream(CurrentText.Length * 2)) { tr.Save(ms, DataFormats.Rtf); string rtfText = ASCIIEncoding.Default.GetString(ms.ToArray()); SetRtfText(rtfText); } } void SetRtfText(string value) { IsSettingText = true; try { RtfText = value; } finally { IsSettingText = false; } } } }
Window1.xaml
XAML
<Window x:Class="GridApplication.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars" xmlns:local="clr-namespace:GridApplication" Title="Window1" Height="600" Width="1000"> <dxb:BarManager CreateStandardLayout="True"> <dxb:BarManager.Items> <dxb:BarButtonItem x:Name="undoItem" Command="Undo" Content="Undo" /> <dxb:BarButtonItem x:Name="redoItem" Command="Redo" Content="Redo" /> <dxb:BarButtonItem x:Name="boldItem" Command="ToggleBold" Content="Bold" /> <dxb:BarButtonItem x:Name="italicItem" Command="ToggleItalic" Content="Italic" /> <dxb:BarButtonItem x:Name="underlineItem" Command="ToggleUnderline" Content="Underline" /> </dxb:BarManager.Items> <dxb:BarManager.Bars> <dxb:Bar UseWholeRow="True"> <dxb:Bar.DockInfo> <dxb:BarDockInfo ContainerType="Top" /> </dxb:Bar.DockInfo> <dxb:Bar.ItemLinks> <dxb:BarButtonItemLink BarItemName="undoItem" /> <dxb:BarButtonItemLink BarItemName="redoItem" /> <dxb:BarButtonItemLink BarItemName="boldItem" /> <dxb:BarButtonItemLink BarItemName="italicItem" /> <dxb:BarButtonItemLink BarItemName="underlineItem" /> </dxb:Bar.ItemLinks> </dxb:Bar> </dxb:BarManager.Bars> <dxg:GridControl Name="grid"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="ID" /> <dxg:GridColumn FieldName="Trademark" /> <dxg:GridColumn FieldName="Model" /> <dxg:GridColumn Header="Description" FieldName="RtfContent" AllowGrouping="False" Width="400"> <dxg:GridColumn.CellDisplayTemplate> <DataTemplate> <local:RichTextBoxEx BorderThickness="0" RtfText="{Binding Path=Value, Mode=OneWay}" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" /> </DataTemplate> </dxg:GridColumn.CellDisplayTemplate> <dxg:GridColumn.CellEditTemplate> <DataTemplate> <local:RichTextBoxEx BorderThickness="0" RtfText="{Binding Path=Value, Mode=TwoWay}" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible" /> </DataTemplate> </dxg:GridColumn.CellEditTemplate> </dxg:GridColumn> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView Name="view" GetActiveEditorNeedsKey="view_GetActiveEditorNeedsKey" ProcessEditorActivationAction="view_ProcessEditorActivationAction"/> </dxg:GridControl.View> </dxg:GridControl> </dxb:BarManager> </Window>
Window1.xaml.cs(vb)
C#
using System.Data; using System.Windows; using System.Windows.Input; using DevExpress.Xpf.Editors; namespace GridApplication { public partial class Window1 : Window { public Window1() { InitializeComponent(); DataSet dataSet = new DataSet(); dataSet.ReadXml("Cars2.xml"); grid.ItemsSource = dataSet.Tables[0]; } private void view_GetActiveEditorNeedsKey(object sender, DevExpress.Xpf.Grid.GetActiveEditorNeedsKeyEventArgs e) { if(e.Column.FieldName == "RtfContent") { if(e.Key == Key.Enter && !e.Modifiers.HasFlag(ModifierKeys.Control)) e.NeedsKey = true; } } private void view_ProcessEditorActivationAction(object sender, DevExpress.Xpf.Grid.ProcessEditorActivationActionEventArgs e) { if(e.Column.FieldName == "RtfContent") { if(e.ActivationAction == ActivationAction.MouseLeftButtonDown) e.RaiseEventAgain = true; } } } }

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.