ALLINSIGHT

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.

Update:
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 :

, ,

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

  • IanM says:

    Thank you! I have been looking for this solution.

  • Akwin Dhas says:

    if i press the enter button in rich text box the cursor should not focus the next text box before 3 enter keys
    in my rich text box’s text look like the following type
    22/117,west street;
    Azhickal;
    Nagercoil.
    How can i write the code for that condition plz help me

  • savi says:

    Uwe,
    Since you solved my problem easily, I need one more help. Each letter/character in my richtextbox occupies different space, so even though I have same number of characters on each line, they dont look aligned. See the example below. In my application they are all of not aligned. How do I solve this problem?
    000: AC7A49D5
    032: 5A275C72
    064: 5696A66E
    096: 18D2CD3D
    128: 561A9566
    160: C353A8F4
    192: C89B3935

    • Uwe says:

      Hi Savi,

      in order to align all characters you need to use a fixed-width (monospace) font, where every character uses the same space.
      “Courier New” is a good choice.

      Since you can have multiple areas with different fonts and colors in a single richtext-document, you need to add a “run” with the font you want:

      • savi says:

        Perfect!

        • savi says:

          This does not work, is there an alternative solution?

          • Uwe says:

            Oh i see, you need to set the font in every new element if you add it in C# code.

            I hope this complete example is more helpful:

  • savi says:

    I am adding Richtextbox dynamically in my code, how do I solve this problem, I mean how do I set the Margin property in the code rather than XAML?

    • Uwe says:

      Hi Savi

      Thank you for your question.
      To set the margin of a RichTextBox dynamically in your code, you have to create a style and add it as resource to your Control.
      Here is an example:

      I hope that answers your question.

Leave a Reply

Your email address will not be published. Required fields are marked *