C#/WPF: Disable Text-Wrap of RichTextBox

前端 未结 6 1531
醉酒成梦
醉酒成梦 2020-12-01 20:44

Does anyone know how I can disable the text wrapping of a RichTextBox? E.g. if I have a large string which doesn\'t fit in the window, the RichTextBox

相关标签:
6条回答
  • 2020-12-01 21:13

    If you don't want to show the horizontal scrollbar, enforce a MinWidth on the ScrollViewer:

    <RichTextBox ScrollViewer.HorizontalScrollBarVisibility="Hidden">
    
        <RichTextBox.Resources>
            <Style TargetType="ScrollViewer">
                <Setter Property="MinWidth" Value="2000" />
            </Style>
        </RichTextBox.Resources>
    
    </RichTextBox>
    
    0 讨论(0)
  • 2020-12-01 21:20

    VerticalScrollBar :

    VerticalScrollBarVisibility="Auto" MaxHeight="200"

    HorizontalScrollBar :

    HorizontalScrollBarVisibility="Auto" MaxWidth="400"

    0 讨论(0)
  • 2020-12-01 21:22

    A RichTextBox in WPF is simply an editor for a FlowDocument.
    According to MSDN:

    Text always wraps in a RichTextBox. If you do not want text to wrap then set the PageWidth on the FlowDocument to be larger than the width of the RichTextBox. However, once the page width is reached the text still wraps.

    So, while there's no way for you to explicitly disable the word-wrapping of a RichTextBox, you can do something like this:

    richTextBox1.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
    richTextBox1.Document.PageWidth = 1000;
    

    Which will have essentially the same desired effect until you have a line that exceeds the PageWidth.

    Note (as of July 2015): VS2015 RC allows wordwrap = false to work precisely as OP seems to desire. I believe earlier versions of Visual Studio did also.

    0 讨论(0)
  • 2020-12-01 21:22

    I also needed to display a large string and tried the RichTextBox but I did not like the solution with setting the PageWidth of the Document to a fixed size. The scrollbar would be visible all the time and the scrolling area was be to big.

    If a TextBlock is sufficient you can use that instead and place it inside a ScrollViewer. It worked perfect for me since I did not need all the extra features of the RichTextBox.

    <ScrollViewer Width="200"
                  Height="100"
                  HorizontalScrollBarVisibility="Auto"
                  VerticalScrollBarVisibility="Auto">
                      <TextBlock TextWrapping="NoWrap">
                          <TextBlock.Text>
                              Very long text Very long text Very long text 
                          </TextBlock.Text>
                      </TextBlock>
    </ScrollViewer>
    
    0 讨论(0)
  • 2020-12-01 21:32

    Suitable solution for me. The idea was taken from here. I defined in XAML

                <RichTextBox x:Name="PART_rtb" VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Auto" TextChanged="MyRichTextBox_OnTextChanged">
                    <RichTextBox.Document>
                        <FlowDocument x:Name="PART_fd"  >
                            <FlowDocument.Resources>
                                <!--This style is used to set the margins for all paragraphs in the FlowDocument to 0.-->
                                <Style TargetType="{x:Type Paragraph}">
                                    <Setter Property="Margin" Value="3"/>
    
                                </Style>
                            </FlowDocument.Resources>
                        </FlowDocument>
                    </RichTextBox.Document>
                </RichTextBox>
    

    In Code

       private void MyRichTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            double i  = PART_rtb.Document.GetFormattedText().WidthIncludingTrailingWhitespace + 20;
            (sender as RichTextBox).Document.PageWidth = i;
        }
    
    0 讨论(0)
  • 2020-12-01 21:37

    Since no answer was satisfying for me, here is my solution:

    private void RichTxt_TextChanged(object sender, TextChangedEventArgs e)
    {
        string text = new TextRange(richTxt.Document.ContentStart, richTxt.Document.ContentEnd).Text;
        FormattedText ft = new FormattedText(text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(richTxt.FontFamily, richTxt.FontStyle, richTxt.FontWeight, richTxt.FontStretch), richTxt.FontSize, Brushes.Black);
        richTxt.Document.PageWidth = ft.Width + 12;
        richTxt.HorizontalScrollBarVisibility = (richTxt.Width < ft.Width + 12) ? ScrollBarVisibility.Visible : ScrollBarVisibility.Hidden;
    }
    

    Question is about performance depending on text length and how often it is refreshed.

    0 讨论(0)
提交回复
热议问题