如何设置<Linebreak />

问题描述 投票:0回答:4

有人知道如何设置

<LineBreak />
内部
<TextBlock />
的高度吗?我尝试更改
TextBlock
的字体大小,但这对我没有帮助。

更新

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

c# .net wpf text
4个回答
9
投票

唯一的方法我可以看到的可能性之一是使用FlowDocumentScrollViewer作为TextBlock的内容。它将允许您使用具有 FontSize 和 LineHeight 属性的 Paragraph 对象的 FlowDocument。这将使您能够在一定程度上改变换行符的高度,这可能不会像您想要的那么小。

<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


添加一些附加信息。我相信您在行之间看到的大部分间隙与文本行的行高有关。我又尝试了一下并想出了这个。它还具有不需要流程文档的额外好处。

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

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

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

又快又脏,但它符合我的目的。


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

0
投票

你不能单独设置

<LineBreak />
的高度,但还有另一种方法可以解决这个问题,那就是把问题从内到外:你可以在
<Run>
内使用具有适当字体大小的
<TextBlock>
,这样您就可以缩小用于计算
<LineBreak />
高度的字体大小。如果您使用复杂的样式规则,这可能会带来麻烦,但如果您愿意内联声明字体大小,它会非常有效,如下面的纯 XAML 示例所示:

<TextBlock FontSize="8">
    <Run FontSize="16">I am some text!</Run><LineBreak />
    <LineBreak />
    <Run FontSize="16">I am some more text!</Run>
</TextBlock>

在此示例中,

<LineBreak />
元素将使用 size-8 字体并显示为正常高度的一半,但运行将使用 size-16 字体(这可能是此应用程序中的正常字体大小)。此解决方案将文本保持为单个
<TextBlock>
,避免使用
<StackPanel>
<Grid>
<FlowDocument>
的复杂性,同时仍然允许您改变段落间距。我已经在从 1 像素(最小字体大小)到 100 的多种字体大小下测试了这项技术,它效果很好,甚至可以正确支持
TextWrapping

© www.soinside.com 2019 - 2024. All rights reserved.