如何设置 <Linebreak /> 的高度

10

有人知道怎样设置<TextBlock /><LineBreak />的高度吗?我试过改变TextBlock的字体大小,但这并没有帮助到我。

更新

我需要减小高度而不是增加。

3个回答

9

唯一的方式我能看到的可能性之一是使用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>

这给我带来了如下结果。它允许你比以往更小地进行操作。

图片描述在此输入


感谢您提供详细的解决方案。但是对于我来说有些繁琐,因为我只需要两个楼层标题。所以,我在StackPanel中使用了两个TextBlock,而不是一个。 - Y.Yanavichus

1

我遇到了同样的问题,对我来说最简单的解决方法是为每一行使用一个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>

你可以通过将边距样式放在共享资源中来清理它。
快速而简单,但对我的目的起了作用。

enter image description here


0

当我面临同样的问题时,我想出了一个可怕的黑客方法:

// 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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接