Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBlock TextWrapping not wrapping

Tags:

wpf

xaml

When I place a TextBlock inside of a Horizontally Aligned StackPanel it does not wrap. I realize that this is because the available width of the StackPanel is PositiveInfinity but are there any workarounds?

My layout is much more complicated than this sample so I cannot remove the StackPanel or the Horizontal Orientation. I just tried to reproduce the simplest possible example that exhibits the behavior.

    <StackPanel Orientation="Horizontal">         <Rectangle Width="50" Height="50" Fill="Blue" VerticalAlignment="Top" />         <Rectangle Width="50" Height="50" Fill="Red" VerticalAlignment="Top" />         <TextBlock TextWrapping="Wrap"                 Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />     </StackPanel> 

Update: The width of the TextBlock must be dynamic. I need it to flow with the window as it is resized.

Update 2: Added another element to the StackPanel because I need the children laid out Horizontally.

Update 3 (Solution): Replaced the StackPanel with a DockPanel.

<DockPanel>     <DockPanel DockPanel.Dock="Top">         <Rectangle Width="50" Height="50" Fill="Blue" VerticalAlignment="Top" DockPanel.Dock="Left" />         <Rectangle Width="50" Height="50" Fill="Red" VerticalAlignment="Top" DockPanel.Dock="Left" />         <TextBlock TextWrapping="Wrap"                 Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />     </DockPanel> </DockPanel> 
like image 705
Shaun Bowe Avatar asked Jul 08 '11 13:07

Shaun Bowe


People also ask

How do I wrap text in TextBlock WPF?

In WPF, the Label control does not support text wrapping. If you need a label that wraps contents across multiple lines, you can use a TextBlock control. Place a TextBlock control inside a Label and apply wrapping on TextBlock.

Is TextBlock editable?

TextBlock is not editable.

How do I create a line break in TextBlock WPF?

Adding Line Breaks Sometimes you will want to insert a line break within a TextBlock. You can do this with a LineBreak inline, added as an XAML element within the text. A new line will be started at the position of this element.


Video Answer


2 Answers

It's because you're using Horizontal orientation on the StackPanel. That means that the StackPanel is giving full width to each child control, and then laying them out horizontally - even if that means exceeding its bounded/visible width. Because there's nothing to constrain the width of the TextBlock, it doesn't wrap.

If you switch to Vertical orientation then the wrapping works, but I'm guessing there's a reason for you specifying otherwise. Can you show what layout you're trying to achieve?

like image 114
Dan Puzey Avatar answered Sep 18 '22 12:09

Dan Puzey


You could use a grid instead of the StackPanel (which as explained does not restrict its content.). A grid allows much more control over layout of items than a StackPanel does and if your image is collapsed then the 'auto' column will have a 0 width.

<DockPanel>     <Grid>         <Grid.ColumnDefinitions>             <ColumnDefinition Width="Auto"/>             <ColumnDefinition Width="*"/>         </Grid.ColumnDefinitions>         <Rectangle Width="50" Height="50" Fill="Blue" VerticalAlignment="Top" />         <TextBlock TextWrapping="Wrap" Grid.Column="1"             Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />     </Grid>  </DockPanel> 
like image 29
Leom Burke Avatar answered Sep 20 '22 12:09

Leom Burke