2

I am having a problems with text wrapping in WPF's RichTextBox, when I get the text like:

  TextRange tr = new TextRange(rtb.Document.ContentStart,
                               rtb.Document.ContentEnd);

tr.Text doesnt have any "\r\n", but in visual control it looks like it should be (it wraps when input reaches RichTextBox border (inserts end of the line)).

I create RichTextBox like this:

 <RichTextBox Name="Rtb"  AcceptsReturn="True" AcceptsTab="True"   VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  
    HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Height="100" Margin="15,125,5,45"  Width="272">
            <FlowDocument  LineHeight="1" IsColumnWidthFlexible="true" >
                <Paragraph LineStackingStrategy="MaxHeight">
                    <Run Text="RichTextBox"/>
                </Paragraph>
            </FlowDocument>
 </RichTextBox>
AutumnKnight
  • 53
  • 3
  • 6
  • The wrapping doesn't actually insert a carriage return/new line to the text. If you change the width of the text box then the point at which it wraps will change. – ChrisF Feb 10 '13 at 12:51
  • how do i detect the wrap event then? – AutumnKnight Feb 10 '13 at 13:04
  • I've been looking, but haven't found anything yet. The only thing I can think of is to set the caret position to each character in the string in turn and then get the line of text that it corresponds to. When the line changes that's your new line. – ChrisF Feb 10 '13 at 13:08

1 Answers1

0

You have to place the RichTextBox in a ScrollViewer.

<ScrollViewer>
   <RichTextBox>
        ...
   </RichTextBox>
</ScrollViewer>
Bizhan
  • 16,157
  • 9
  • 63
  • 101