Donotalo
Donotalo

Reputation: 13025

RichTextBox showing texts vertically

Following XAML code:

<ScrollViewer HorizontalScrollBarVisibility="Auto"
              VerticalScrollBarVisibility="Auto">

    <DockPanel LastChildFill="True">
        <TextBox Name="textBox1" DockPanel.Dock="Top" />

        <GroupBox Header="All Events" DockPanel.Dock="Top">
            <RichTextBox Margin="5" Name="richTextBoxEvents"
                         HorizontalScrollBarVisibility="Auto"
                         VerticalScrollBarVisibility="Auto" />
        </GroupBox>
    </DockPanel>
</ScrollViewer>

causes everything displayed in the RichTextBox vertically!

Vertical text in RichTextBox

I want the text to appear horizontally. Why is this appearing vertically and how can I fix that?

I'm using C#, WPF and .NET 4.

EDIT

If the ScrollViewer is taken away, then the text appear horizontally. But I need the scroll viewer. What's the solution then?

Upvotes: 6

Views: 4061

Answers (2)

Naren
Naren

Reputation: 2311

A similar scenario can be found here.

Your problem can be solved by setting the width of the RichTextBox.

  1. By setting some arbitrary width.

    <RichTextBox Margin="5" Name="richTextBoxEvents"
                     HorizontalScrollBarVisibility="Auto"
                     VerticalScrollBarVisibility="Auto"
                     Width="100" />
    
  2. Or you can assign the width of the Parent.

    <RichTextBox Margin="5" Name="richTextBoxEvents"
                     HorizontalScrollBarVisibility="Auto"
                     VerticalScrollBarVisibility="Auto"
                     Width="{Binding RelativeSource={RelativeSource FindAncestor, 
                     AncestorType=Window, AncestorLevel=3}, Path=ActualWidth}" />
    

Here you can change the ancestor level depending on your needs,I bind the RichTextBox width to the Window's width and AncestorLevel is 3.

Upvotes: 10

Nathan Dace
Nathan Dace

Reputation: 1555

RichTextBox will always word wrap, so I'm assuming that the ScrollViewer is forcing it to start word wrapping.

If you remove the auto visibility, then it works as expected and you'll still get your scrollbars:

<ScrollViewer>
        <DockPanel LastChildFill="True">
            <TextBox Name="textBox1" DockPanel.Dock="Top" />
            <GroupBox Header="All Events" DockPanel.Dock="Top">
                <RichTextBox Margin="5" Name="richTextBoxEvents" />
            </GroupBox>
        </DockPanel>
    </ScrollViewer>

Upvotes: 1

Related Questions