Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a WPF TextBlock show my text on multiple lines?

Tags:

wpf

xaml

i have a wpf window where i have a stackpanel with two viewports in it - each Viewport with a textblock in it.

<Grid>     <StackPanel VerticalAlignment="Center" Orientation="Vertical" >         <Viewbox Margin="100,0,100,0">             <TextBlock x:Name="headerText" Text="Lorem ipsum dolor" Foreground="Black"/>         </Viewbox>         <Viewbox Margin="150,0,150,0">             <TextBlock x:Name="subHeaderText" Text="Lorem ipsum dolor, Lorem ipsum dolor, lorem isum dolor, Lorem ipsum dolor, Lorem ipsum dolor, lorem isum dolor, " TextWrapping="Wrap" Foreground="Gray" />         </Viewbox>     </StackPanel> </Grid> 

What i would like to achieve is that the top textblock is the heading with a bigger text. The second textblock is the sub heading with a smaller text. No matter how much text there is for either the heading or the subheading the font should dynamic become smaller/bigger. My problem is that i would like the subheading to be fixed width. This means that, the font should be a percentage (70%) of the heading and wrap to multiple lines, depending on how much text i have. I enclosed the code I have thus far... im missing something with that subheading, cant figure out what. Cheers

Edit Basically what i want achieve is that the sub header wraps the text so it can expand it downwards with the font being a 70% of the heading - no matter how big that font is.

like image 212
Brian Hvarregaard Avatar asked Sep 13 '11 18:09

Brian Hvarregaard


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.

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.

Is TextBlock editable in WPF?

TextBlock is not editable.

How do I make a TextBox readonly in WPF?

To prevent users from modifying the contents of a TextBox control, set the IsReadOnly attribute to true.


2 Answers

Nesting a stackpanel will cause the textbox to wrap properly:

<Viewbox Margin="120,0,120,0">     <StackPanel Orientation="Vertical" Width="400">         <TextBlock x:Name="subHeaderText"                     FontSize="20"                     TextWrapping="Wrap"                     Foreground="Black"                    Text="Lorem ipsum dolor, lorem isum dolor,Lorem ipsum dolor sit amet, lorem ipsum dolor sit amet " />     </StackPanel> </Viewbox> 
like image 195
Brian Hvarregaard Avatar answered Sep 20 '22 15:09

Brian Hvarregaard


Use the property TextWrapping of the TextBlock element:

<TextBlock Text="StackOverflow Forum"            Width="100"            TextWrapping="WrapWithOverflow"/> 
like image 45
Tonatio Avatar answered Sep 22 '22 15:09

Tonatio