Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set height of <Linebreak />

Tags:

c#

.net

text

wpf

Does anybody know how to set up height of <LineBreak /> inside <TextBlock />? I tried to change font size of TextBlock but it's didn't help me.

UPDATE

I need to decrease it, not increase.

like image 275
Y.Yanavichus Avatar asked Feb 09 '12 09:02

Y.Yanavichus


2 Answers

The only way One of the possibility's that I can see are to use FlowDocumentScrollViewer as the Content of your TextBlock. It will allow you to use a FlowDocument which has a Paragraph object which has FontSize and LineHeight Property's. This will give you the ability to change the Height of the LineBreak to a certain extent, this may not be as small as you want.

<Grid>
   <TextBlock LineHeight="1" Height="85" Width="400" HorizontalAlignment="Left" Margin="12,29,0,0" Name="textBlock1" VerticalAlignment="Top" Background="Beige" >
        <FlowDocumentScrollViewer Width="400" VerticalScrollBarVisibility="Hidden" >
            <FlowDocument>
                <Paragraph LineHeight="1" FontSize="12" FontFamily="Arial" Foreground="Red" >
                    <Run> This is a Test of line height</Run>
                 </Paragraph>
                <Paragraph LineHeight="1" FontSize="1"  BorderThickness=" 1" BorderBrush="Black">
                    <LineBreak/>
                </Paragraph >
                <Paragraph LineHeight="1" FontSize="12" FontFamily="Arial" Foreground="Blue">
                    <Run> This is a Test of line height</Run>
                </Paragraph> 
                <Paragraph LineHeight="1" FontSize="2"  BorderThickness=" 1" BorderBrush="Black">
                    <LineBreak />
                </Paragraph>
                <Paragraph   LineHeight="1" FontSize="12" FontFamily="Arial" Foreground="Green" >  
                    <Run> This is a Test of line height</Run>
                </Paragraph>
                <Paragraph LineHeight="1" FontSize="5"  BorderThickness=" 1" BorderBrush="Black">
                    <LineBreak />
                </Paragraph>
            </FlowDocument>
        </FlowDocumentScrollViewer>
    </TextBlock>
</Grid>

This gave me a result like this.

enter image description here


To add some additional information. I beleive most of the gap that you see between lines has to do with the LineHeight of the Text Lines. I played around with it a litte bit more and came up with this. It also has the added benefit of not needing a flow document.

<TextBlock LineHeight="9.75" LineStackingStrategy="BlockLineHeight" Margin="12,188,-12,-188">
    <Run> This is a Test of Line Height</Run>
    <LineBreak />
    <Run >This is a Test of Line Height</Run>
    <LineBreak />
    <Run>This is a Test of Line Height</Run>
    <LineBreak />
    <Run> This is a Test of Line Height</Run>
</TextBlock>

This gave me a result that looks like this. It will allow you to go smaller than you could otherwise

enter image description here

like image 188
Mark Hall Avatar answered Oct 06 '22 04:10

Mark Hall


I had the same problem, easiest workaround for me was to use a TextBlock for each line, give the TextBlock a bottom margin setting and contain them in a StackPanel.

<StackPanel>
    <TextBlock Margin="0,0,0,10">
        This is the text and this text is quite long so it wraps over the end of the line...
    </TextBlock>
    <TextBlock Margin="0,0,0,10">
        This is the text and this text is quite long so it wraps over the end of the line...
    </TextBlock>
</StackPanel>

You can clean it up by putting the margin style in a shared resource.

Quick and dirty but it worked for my purposes.

enter image description here

like image 40
Richard Moore Avatar answered Oct 06 '22 03:10

Richard Moore