Reputation: 3312
I copied the code example from the RichTextBox help into an empty WPF window like this:
<Window x:Class="RichTextBoxWidthProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<RichTextBox>
<FlowDocument>
<Paragraph>
This is flow content and you can
<Bold>edit me!</Bold>
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
</Window>
The result looks rather strange:
Why is there only 1 character written per line and how can I fix this ?
Upvotes: 2
Views: 751
Reputation: 3312
After spending a day trying to find the reason for this problem, I just run the application and the result looks like this:
The text uses all available space and gets reformatted if the Window size changes. This basically means there is no real problem with my code, but with the WPF designer in Visual Studio. After some further investigation I found out that in the designer, the Paragraph is only zero pixel wide.
If you want to see also in the designer a reasonable display, give a Width to Window, Grid or RichTextBox. But, of course, the text will then no longer use all the available space, but only the width you defined, which might not be what you want. If you want the width only to be used by the designer, but not during runtime, add the 'd:' before Width:
<Window x:Class="RichTextBoxWidthProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" d:Width="333">
Please read this first before marking this as duplicate
I made the unfortunate experience that often a question gets marked a duplicate, when it is actually not. I am aware that there are several questions regarding FlowDocument not using the available width, but please note that most recommend to set ColumnWidth or some other width, which would actually then be correctly displayed in the Designer, but the running application would be limited to that width, which can be rather annoying (no resizing of the FlowDocument when the Window is made bigger).
Therefore it is important that an answer is on Stackoverflow explaining that it is in this case just a designer problem and not really a width problem.
Upvotes: 3