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.
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.
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.
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
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.
You can't set the <LineBreak />
height independently, but there's another way to go about it, which is to turn the problem inside-out: You can use <Run>
s with the proper font size inside the <TextBlock>
, so you can then shrink the font size used to calculate the <LineBreak />
height. This may cause trouble if you're using complex style rules, but it works really well if you're willing to have the font size declared inline, as in the pure-XAML example below:
<TextBlock FontSize="8">
<Run FontSize="16">I am some text!</Run><LineBreak />
<LineBreak />
<Run FontSize="16">I am some more text!</Run>
</TextBlock>
In this example, the <LineBreak />
elements will use the size-8 font and appear half as tall as normal, but the runs will use a size-16 font (which is presumably the normal font size in this application). This solution keeps the text as a single <TextBlock>
, avoiding the complexities of using <StackPanel>
or <Grid>
or <FlowDocument>
, while still allowing you to vary the paragraph spacing. I've tested this technique at lots of font sizes from 1 pixel (the minimum font size) up to 100, and it works well, and even supports TextWrapping
properly.
Here's an awful hack I came up with when faced with the same problem:
// close out paragraph and move to next line
textBlock.Inlines.Add(new LineBreak());
var span = new Span();
// use a smaller size so there's less of a gap to the next paragraph
span.FontSize = 4;
// super awful hack. Using a space here won't work, but tab does
span.Inlines.Add(new Run("\t"));
// now the height of this line break will be governed by the font size we set above, not by the font size of the main text
span.Inlines.Add(new LineBreak());
textBlock.Inlines.Add(span);
© 2022 - 2024 — McMap. All rights reserved.