Search code examples
c#.netwpftext

How to set height of <Linebreak />


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.


Solution

  • 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