有人知道怎样设置<TextBlock />
内<LineBreak />
的高度吗?我试过改变TextBlock
的字体大小,但这并没有帮助到我。
更新
我需要减小高度而不是增加。
唯一的方式我能看到的可能性之一是使用FlowDocumentScrollViewer作为TextBlock的内容。它将允许您使用FlowDocument,该文档具有Paragraph对象,该对象具有FontSize和LineHeight属性。这将使您能够在一定程度上更改LineBreak的高度,但这可能不会像您想要的那样小。
<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>
这给我带来了以下结果。
为了添加一些额外的信息。我相信你看到的大部分行之间的间隙都与文本行的行高有关。我稍微调整了一下,得到了这个结果。它还具有不需要流程文档的附加好处。
<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>
我遇到了同样的问题,对我来说最简单的解决方法是为每一行使用一个TextBlock,在TextBlock中设置底部边距,并将它们包含在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>
当我面临同样的问题时,我想出了一个可怕的黑客方法:
// 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);