如何设置<linebreak> </linebreak>的高度

时间:2012-02-09 09:21:50

标签: c# .net wpf text

有人知道如何在<LineBreak />内设置<TextBlock />的高度吗?我试图更改TextBlock的字体大小,但这对我没有帮助。

更新

我需要减少它,而不是增加。

3 个答案:

答案 0 :(得分:8)

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

这给了我这样的结果。

enter image description here


添加一些其他信息。我相信你在线之间看到的大部分差距都与文本行的LineHeight有关。我玩了一点点,然后想出了这个。它还具有不需要流程文档的额外好处。

<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>

这给了我一个看起来像这样的结果。它会让你比你原来要小吗

enter image description here

答案 1 :(得分: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);

答案 2 :(得分:0)

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