RichTextBox with HorizontalAlignment="Stretch"

Feb 9, 2011 at 7:06 PM

I have a RTB in a grid with the HorizontalAlignment="Stretch" set, and no explicit width.  When I do this, I end up with all the text that is typed into the RTB in a single column scrolling down vertically.  Here's the XAML:

            <!-- Comments -->
            <Grid Margin="2" DockPanel.Dock="Top">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200" SharedSizeGroup="A" />
                    <ColumnDefinition Width="*" SharedSizeGroup="B" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,4,0" Text="Comments" />
                <extToolkit:RichTextBox Grid.Column="1" Height="75" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" Text="{Binding Comments}" Margin="2,2,4,4">
                    <extToolkit:RichTextBoxFormatBarManager.FormatBar>
                        <extToolkit:RichTextBoxFormatBar />
                    </extToolkit:RichTextBoxFormatBarManager.FormatBar>
                </extToolkit:RichTextBox>

            </Grid>

Is this a bug or am I doing something wrong?

Feb 9, 2011 at 8:55 PM

I took the code you provided and pasted it exactly.  I could not reproduce your issue.   The behavior you describe is usually due to some type of layout problem.  Take this same code and place it all by itself in a new window and see if it still happens.

<Window x:Class="WPFToolkit.Extended.Samples.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
        Title="MainWindow" Height="350" Width="525" >

    <Grid Margin="2" DockPanel.Dock="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" SharedSizeGroup="A" />
            <ColumnDefinition Width="*" SharedSizeGroup="B" />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,4,0" Text="Comments" />
        <extToolkit:RichTextBox Grid.Column="1" Height="75" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" Text="{Binding Comments}" Margin="2,2,4,4">
            <extToolkit:RichTextBoxFormatBarManager.FormatBar>
                <extToolkit:RichTextBoxFormatBar />
            </extToolkit:RichTextBoxFormatBarManager.FormatBar>
        </extToolkit:RichTextBox>
    </Grid>
    
</Window>

The above code works properly.  So something else in your view may be causing the issue.

Mar 13, 2012 at 7:47 AM

I have the same problem with CodingGorilla 

My Xaml structure is like this:

Scrollviewer > StackPanel > [ some control + a RichTextBox]

I set my scrollviewer to use a style like this

<Style x:Key="ScrollViewerStyle" TargetType="{x:Type ScrollViewer}">        
   <Setter Property="Background" Value="lavender"/>        
   <Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>        
   <Setter Property="IsEnabled" Value="true"></Setter>    
</Style>

After I remove the style from my scrollViewer, the problem solved!

I don't know why but I still hope it will help other who have the same problem.