【问题标题】:How to set height of <Linebreak />如何设置 <Linebreak /> 的高度
【发布时间】:2012-03-01 18:18:52
【问题描述】:

有人知道如何在&lt;TextBlock /&gt; 中设置&lt;LineBreak /&gt; 的高度吗?我尝试更改 TextBlock 的字体大小,但没有帮助。

更新

我需要减少而不是增加。

【问题讨论】:

    标签: c# .net wpf text


    【解决方案1】:

    唯一的方法 我可以看到的一种可能性是使用FlowDocumentScrollViewer 作为TextBlock 的内容。它将允许您使用具有 Paragraph 对象的 FlowDocument,该对象具有 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>
    

    这给了我这样的结果。


    添加一些附加信息。我相信您在行之间看到的大部分间隙都与文本行的 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>
    

    这给了我一个看起来像这样的结果。它会让你变得比其他方式更小

    【讨论】:

    • 感谢您的详细解决方案。它很重,所以它不适合我:我只想要两个楼层标题。因此,我在 StackPanel 中使用了两个而不是一个 TextBlock。
    【解决方案2】:

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

    您可以通过将边距样式放在共享资源中来清理它。

    又快又脏,但它适用于我的目的。

    【讨论】:

      【解决方案3】:

      这是我在遇到同样问题时想出的一个可怕的技巧:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-28
        • 2012-01-28
        • 2011-01-18
        • 1970-01-01
        相关资源
        最近更新 更多