Home of the AlmostImplementedException

WPF – RichTextBox: How to change the paragraph spacing / add a single row on enter

If you have ever used the WPF-RichTextBox you know this strang behavior when you press enter. Instead of a single row, the cursor jumps two. You can press shift + enter instead and get just a single row. But don’t try to tell this your customers.
There is an easy and elegant way to change this behavior. When you press enter a paragraph is created and it uses only one row, but it has a margin instead. It just looks like two rows. To get what we want, we just need to change the margin of a paragraph.

And here is the XAML-Code to do it:

Copy the RichTextBox.Resources-Code into your RichTextBox and the problem is solved. The Style just changes the property margin for the paragraph-object to 0.

Here is the answere to Savis question in a more readable way

To add a style to a single RichTextBox like shown above, but in the code behind, you can use the following code:

You can even add this Style to the Window and use it for multiple RichTextBox. Simply create a function for the Loaded-event and add the style to the window rescources:

Or in WPF / XAML

Share :

, ,

Leave a Reply