Setting Background for WPF RichTextBox

15 Jul 20214 minutes to read

The WPF RichTextBox control allows you to change background color of the control. A background of a control is represented by Background property of SfRichTextBoxAdv class. The default value of this property is black.

The following code illustrates how to apply color as background to the document.

<RichTextBoxAdv:SfRichTextBoxAdv x:Name="richTextBoxAdv" Background="#6699cc" />
// Initializes a new instance of RichTextBoxAdv.
SfRichTextBoxAdv richTextBoxAdv = new SfRichTextBoxAdv();
// Sets the control background color
richTextBoxAdv.Background = new SolidColorBrush(Color.FromRgb(102, 153, 204));
' Initializes a new instance of RichTextBoxAdv.
Dim richTextBoxAdv As New SfRichTextBoxAdv()

' Sets the control background color.
richTextBoxAdv.Background = new SolidColorBrush(Color.FromRgb(102, 153, 204))

Pages layout
Changing Background color of Page Layout in WPF RichTextBox

Continuous layout
Changing Background color of Continuous Layout in WPF RichTextBox

Block layout
The block layout always inherits the control background color.
Changing Background color of Block Layout in WPF RichTextBox

How to override the document background in continuous layout type?

By default, the document background properties will be applied when the LayoutType is continuous. You can suppress the document background and apply the control background by setting OverridesDocumentBackground property to true. The default value of this property is false.

NOTE

This property is valid only when the LayoutType is continuous.

The following code illustrates how to override the document background color.

<RichTextBoxAdv:SfRichTextBoxAdv x:Name="richTextBoxAdv" LayoutType="Continuous" Background="#6699cc" OverridesDocumentBackground="True" />
// Initializes a new instance of RichTextBoxAdv.
SfRichTextBoxAdv richTextBoxAdv = new SfRichTextBoxAdv();
// Sets the control background color
richTextBoxAdv.Background = new SolidColorBrush(Color.FromRgb(102, 153, 204));
// Sets the layout type as continous 
richTextBoxAdv.LayoutType = LayoutType.Continuous;
//Enable the OverridesDocumentBackground property 
richTextBoxAdv.OverridesDocumentBackground = true;
' Initializes a new instance of RichTextBoxAdv.
Dim richTextBoxAdv As New SfRichTextBoxAdv()
' Sets the control background color.
richTextBoxAdv.Background = new SolidColorBrush(Color.FromRgb(102, 153, 204))
' Sets the layout type as continous 
richTextBoxAdv.LayoutType = LayoutType.Continuous
' Enable the OverridesDocumentBackground property
richTextBoxAdv.OverridesDocumentBackground = true

Continuous layout:
Changing Background color of Continuous Layout in WPF RichTextBox

Setting Background for Document Pages

The RichTextBox control allows you to change background color of the document pages. A background of a document is represented by Background property of DocumentAdv class. The default value of this property is white.

NOTE

  1. This property is independent for a document. So the background will change when the document is changed.
  2. To maintain same background for all documents, you can reset this property in DocumentChanged event.

The following code illustrates how to apply color as background to the document pages.

// Initializes a new instance of RichTextBoxAdv.
SfRichTextBoxAdv richTextBoxAdv = new SfRichTextBoxAdv();
// Sets the document background color
richTextBoxAdv.Document.Background.Color = Color.FromRgb(102, 153, 204);
' Initializes a new instance of RichTextBoxAdv.
Dim richTextBoxAdv As New SfRichTextBoxAdv()
' Sets the document background color.
richTextBoxAdv.Document.Background.Color = Color.FromRgb(102, 153, 204)

Pages layout:
Changing Background color of Page Layout in WPF RichTextBox

Continuous layout:
Changing Background color of Continuous Layout in WPF RichTextBox

NOTE

This API is supported starting from release version v17.4.0.X.
You can also explore our WPF RichTextBox example to knows how to render and configure the editing tools.