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.
Files to Review
- RichTextBoxEx.cs (VB: RichTextBoxEx.vb)
- Window1.xaml (VB: Window1.xaml)
- Window1.xaml.cs (VB: Window1.xaml.vb)
Documentation
- ColumnBase.CellDisplayTemplate
- ColumnBase.CellEditTemplate
- DataViewBase.GetActiveEditorNeedsKey
- DataViewBase.ProcessEditorActivationAction
- RichTextBox
More Examples
- WPF Data Grid - Specify Navigation in Custom Cell Editors
- WPF Data Grid - Use Custom Editors to Edit Cell Values
- WPF Data Grid - Assign a ComboBox Editor to a Column
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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;
}
}
}
}
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>
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;
}
}
}
}