29

I have the following XAML code:

<Window x:Class="RichText_Wrapping.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<Grid>
    <RichTextBox Height="100" Margin="2" Name="richTextBox1">
        <FlowDocument>
            <Paragraph>
                This is a RichTextBox - if you don't specify a width, the text appears in a single column
            </Paragraph>
        </FlowDocument>
    </RichTextBox>
</Grid>

... If you create this window in XAML, you can see that when you don't specify a width for the window, it wraps the text in a single column, one letter at a time. Is there something I'm missing? If it's a known deficiency in the control, is there any workaround?

6 Answers 6

30

This is a confirmed bug with the WPF RichTextBox. To fix it, Bind the PageWidth of the FlowDocument to the RichTextBox width, i.e.

<RichTextBox Name="rtb">
    <FlowDocument Name="rtbFlowDoc" PageWidth="{Binding ElementName=rtb, Path=ActualWidth}" />
</RichTextBox>

EDIT: Give the FlowDocument a name so that you can access it in the code behind and never new the flow document in codebehind.

2
  • 3
    Where was this bug confirmed? Are there any other known workarounds? Since I build my FlowDocument dynamically, the PageWidth trick won't work for me.
    – dthrasher
    Jun 23, 2010 at 22:23
  • I ran into a horrible performance hit by doing this, and ended up wrapping my RichTextBox in a Grid so I could bind to Grid.ActualWidth instead. Wish I knew why, but if anyone else runs into the same performance issue, that fixed it for me.
    – Rachel
    Jul 29, 2020 at 20:35
10

Try binding the FlowDocument's width (one way) to the width of the container RichTextBox.

Worked for me...

1
  • 5
    This problem keeps happening with me on and off, so eventually I just caved and named the parent grid to something like "mainGrid" and set Width="{Binding ElementName=mainGrid, Path=ActualWidth}" and Height="{Binding ElementName=mainGrid, Path=ActualHeight}" to force the behavior I want.
    – Michael
    Mar 4, 2009 at 18:28
7

The approach in this article worked for me:

WPF RichTextBox doesn't provide the functionality to adjust its width to the text. As far as I know, RichTextBox use a FlowDocumentView in its visual tree to render the Flowdocument. It will take the available space to render its content, so it won't adjust its size to the content. Since this is an internal class, it seems we cannot override the layout process to let a RichTextBox to adjust its size to the text.

Therefore, I think your approach is in the right direction. Unfortunelately, based on my research, there is no straightforward way to measure the size of the rendered text in a RichTextBox.

There is a workaround we can try. We can loop through the flowdocument in RichTextBox recursively to retrieve all Run and Paragraph objects. Then we convert them into FormattedText to get the size.

This article demonstrates how to convert a FlowDocument to FormattedText. I also write a simple sample using the FlowDocumentExtensions class in that article.

    public Window2()
    {
      InitializeComponent();

      StackPanel layoutRoot = new StackPanel();
      RichTextBox myRichTextBox = new RichTextBox() { Width=20};

      this.Content = layoutRoot;
      layoutRoot.Children.Add(myRichTextBox);

      myRichTextBox.Focus();
      myRichTextBox.TextChanged += new TextChangedEventHandler((o,e)=>myRichTextBox.Width=myRichTextBox.Document.GetFormattedText().WidthIncludingTrailingWhitespace+20);
    }


  public static class FlowDocumentExtensions
  {
    private static IEnumerable<TextElement> GetRunsAndParagraphs(FlowDocument doc)
    {
      for (TextPointer position = doc.ContentStart;
        position != null && position.CompareTo(doc.ContentEnd) <= 0;
        position = position.GetNextContextPosition(LogicalDirection.Forward))
      {
        if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd)
        {
          Run run = position.Parent as Run;

          if (run != null)
          {
            yield return run;
          }
          else
          {
            Paragraph para = position.Parent as Paragraph;

            if (para != null)
            {
              yield return para;
            }
          }
        }
      }
    }

    public static FormattedText GetFormattedText(this FlowDocument doc)
    {
      if (doc == null)
      {
        throw new ArgumentNullException("doc");
      }

      FormattedText output = new FormattedText(
        GetText(doc),
        CultureInfo.CurrentCulture,
        doc.FlowDirection,
        new Typeface(doc.FontFamily, doc.FontStyle, doc.FontWeight, doc.FontStretch),
        doc.FontSize,
        doc.Foreground);

      int offset = 0;

      foreach (TextElement el in GetRunsAndParagraphs(doc))
      {
        Run run = el as Run;

        if (run != null)
        {
          int count = run.Text.Length;

          output.SetFontFamily(run.FontFamily, offset, count);
          output.SetFontStyle(run.FontStyle, offset, count);
          output.SetFontWeight(run.FontWeight, offset, count);
          output.SetFontSize(run.FontSize, offset, count);
          output.SetForegroundBrush(run.Foreground, offset, count);
          output.SetFontStretch(run.FontStretch, offset, count);
          output.SetTextDecorations(run.TextDecorations, offset, count);

          offset += count;
        }
        else
        {
          offset += Environment.NewLine.Length;
        }
      }

      return output;
    }

    private static string GetText(FlowDocument doc)
    {
      StringBuilder sb = new StringBuilder();

      foreach (TextElement el in GetRunsAndParagraphs(doc))
      {
        Run run = el as Run;
        sb.Append(run == null ? Environment.NewLine : run.Text);
      }
      return sb.ToString();
    }
  }
1
  • I used FormattedTextWidth + 20 to prevent wrapping. Sep 11, 2017 at 21:58
3

I copy pasted your code and its not in a single column, Do you have a width somewhere that is small? Maybe defined on the code behind for instance.

1
  • Agreed. There's got to be something else going on, perhaps a style for one of your types defined in your project. The snippet of XAML you've posted displays just fine in XAML Cruncher, for instance.
    – Ryan Lundy
    Dec 8, 2008 at 22:15
3

I noticed that I only had this issue when my default ScrollViewer style explicitly set HorizontalScrollBarVisibility=Hidden.
Removing this setter (default value is Hidden anyway) fixed the single column issue for me in my RichTextBox.

1
  • 1
    Had exactly the same problem. Any value other than HorizontalScrollBarVisibility=Disabled (or completeley removing the setter) causes the single column issue.
    – MatSnow
    Nov 7, 2018 at 8:44
2

Just for the record as I think this thread is missing some explanations as per the why: RichTextBox MeasureOverride implementation is like that. I won't call that a bug, maybe just a poor design behavior justified by the fact that just like mentioned above the FlowDocument is not cheap to measure due to its complexity. Bottom line, avoid unlimited Width constraint by binding MinWidth or wrap it in a limiting container.

    /// <summary>
    /// Measurement override. Implement your size-to-content logic here.
    /// </summary>
    /// <param name="constraint">
    /// Sizing constraint.
    /// </param>
    protected override Size MeasureOverride(Size constraint)
    {
        if (constraint.Width == Double.PositiveInfinity)
        {
            // If we're sized to infinity, we won't behave the same way TextBox does under
            // the same conditions.  So, we fake it.
            constraint.Width = this.MinWidth;
        }
        return base.MeasureOverride(constraint);
    }
1
  • BTW, remember that looking into the source of the Framework HELP like A LOT to understand behaviors that might not be so obvious at first sight. Come on, we all know that there is no such thinks as Bugs... It's only features :) lol. And Kudos to whomever at MS that made the .NET open source happen! Make our life so much easier!
    – VeV
    Feb 14, 2017 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.