views:

1011

answers:

8

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?

Thanks,

Michael

+2  A: 

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.

Artur Carvalho
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.
Kyralessa
A: 

I don't think I'm setting the width anywhere. Here is my xaml.cs file:

using System.Windows;
namespace RichText_Wrapping
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
}

I am using Vista and Visual Studio 2008 with service pack 1. I created a new project to make sure it wasn't a custom style that I set somewhere, but it's still happening! Any suggestions on where to look would be appreciated.

Mick
A: 

I pasted the code directly into XamlPad and it worked fine. All of a sudden the application started working as well. Go figure...

Mick
A: 

Hmm I have the same issue. Setting a width on the RichEdit fixed the 1 letter column. But when there is no width set, it displays one letter per line. I also set the background color to something very flashy, to see the control takes the whole width of the page. (but with only one column in it.

Hmmm

Tom Deleu
+1  A: 

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

Worked for me...

azazeal
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.
Mick
+2  A: 

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 PageWidth="{Binding ElementName=rtb, Path=ActualWidth}" />
</RichTextBox>
geosteve
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
A: 

Thanx geosteve - your solution worked for me!

Gili