TextBlock.LineHeight 属性

定义

获取或设置各行内容的高度。Gets or sets the height of each line of content.

public:
 property double LineHeight { double get(); void set(double value); };
[System.ComponentModel.TypeConverter(typeof(System.Windows.LengthConverter))]
public double LineHeight { get; set; }
member this.LineHeight : double with get, set
Public Property LineHeight As Double

属性值

行的高度范围为 0.0034 到 160000,以与设备无关的像素为单位。The height of line, in device independent pixels, in the range of 0.0034 to 160000. 如果值为 NaN (等效于属性值 "Auto"),则指示行高度由当前字体特征自动确定。A value of NaN (equivalent to an attribute value of "Auto") indicates that the line height is determined automatically from the current font characteristics. 默认值为 NaNThe default is NaN.

属性

异常

LineHeight 设置为非正值。LineHeight is set to a non-positive value.

示例

下面的示例演示如何设置 TextBlock 元素的 LineHeight 特性。The following example shows how to set the LineHeight attribute of a TextBlock element.

<TextBlock 
  Name="textBlock"
  
  Background="AntiqueWhite" 
  Foreground="Navy" 
  
  FontFamily="Century Gothic"
  FontSize="12"
  FontStretch="UltraExpanded"
  FontStyle="Italic"
  FontWeight="UltraBold"
  
  LineHeight="Auto"
  Padding="5,10,5,10"
  TextAlignment="Center"
  TextWrapping="Wrap"
  
  Typography.NumeralStyle="OldStyle"
  Typography.SlashedZero="True"
>
  <Run Background="LightGreen">Text run 1.</Run>
  <LineBreak/><Run Background="LightBlue">Text run 2.</Run>
  <LineBreak/><Run Background="LightYellow">Text run 3.</Run>
</TextBlock>

下面的示例演示如何以编程方式设置 LineHeight 属性。The following example shows how to set the LineHeight property programmatically.

TextBlock textBlock = new TextBlock(new Run("A bit of text content..."));

textBlock.Background              = Brushes.AntiqueWhite;
textBlock.Foreground              = Brushes.Navy;

textBlock.FontFamily              = new FontFamily("Century Gothic");
textBlock.FontSize                = 12;
textBlock.FontStretch             = FontStretches.UltraExpanded;
textBlock.FontStyle               = FontStyles.Italic;
textBlock.FontWeight              = FontWeights.UltraBold;

textBlock.LineHeight              = Double.NaN;
textBlock.Padding                 = new Thickness(5, 10, 5, 10);
textBlock.TextAlignment           = TextAlignment.Center;
textBlock.TextWrapping            = TextWrapping.Wrap;

textBlock.Typography.NumeralStyle = FontNumeralStyle.OldStyle;
textBlock.Typography.SlashedZero  = true;
Dim textBlock As New TextBlock(New Run("A bit of text content..."))

textBlock.Background = Brushes.AntiqueWhite
textBlock.Foreground = Brushes.Navy

textBlock.FontFamily = New FontFamily("Century Gothic")
textBlock.FontSize = 12
textBlock.FontStretch = FontStretches.UltraExpanded
textBlock.FontStyle = FontStyles.Italic
textBlock.FontWeight = FontWeights.UltraBold

textBlock.LineHeight = Double.NaN
textBlock.Padding = New Thickness(5, 10, 5, 10)
textBlock.TextAlignment = TextAlignment.Center
textBlock.TextWrapping = TextWrapping.Wrap

textBlock.Typography.NumeralStyle = FontNumeralStyle.OldStyle
textBlock.Typography.SlashedZero = True

注解

更改此值不会更改关联文本的高度;相反,它更改包含文本的行的高度。Changing this value does not change the height of the associated text; rather, it changes the height of the line that contains the text. 若要更改文本大小,请使用 FontSize 属性。To change the size of the text, use the FontSize property.

除了此属性,TextBlock 中的行布局会受到其 LineStackingStrategy 属性的影响。In addition to this property, the layout of lines in a TextBlock is affected by its LineStackingStrategy property.

此依赖属性还具有附加的属性用法。This dependency property also has an attached property usage. XAMLXAML中,使用情况是 <object TextBlock.LineHeight="value".../>的,其中object是包含在 TextBlock内的对象元素(通常是 flow 元素),是一个字符串,如 XAML 值中所述。In XAMLXAML, the usage is <object TextBlock.LineHeight="value".../>, where object is an object element (typically a flow element) contained within a TextBlock, and value is a string as explained in XAML Values. 在代码中,GetLineHeightSetLineHeight支持附加的属性用法。In code, the attached property usage is supported by GetLineHeight and SetLineHeight. 附加的属性用法并不常见。The attached property usage is not common.

XAML 属性用法XAML Attribute Usage

<object LineHeight="double"/>  
- or -  
<object LineHeight="qualifiedDouble"/>  
- or -  
<object LineHeight="Auto"/>  

XAML 值XAML Values

doubledouble
Double

Double 值的字符串表示形式等于或大于 0.0034,然后 160000String representation of a Double value equal to or greater than 0.0034 and equal to or less then 160000. 非限定值以与设备无关的像素度量。An unqualified value is measured in device independent pixels. 字符串不需要显式包含小数点。Strings need not explicitly include decimal points.

qualifiedDoublequalifiedDouble
如上所述的双精度值(只不过 Auto),后跟以下单元说明符之一: pxincmptA double value as described above, (excepting Auto) followed by one of the following unit specifiers: px, in, cm, pt.

px (默认值)为 与设备无关的单位(每个单位 1/96 英寸)device-independent units (1/96th inch per unit)px (default) is 与设备无关的单位(每个单位 1/96 英寸)device-independent units (1/96th inch per unit)

in 为英寸;1in = = 96pxin is inches; 1in==96px

cm 是厘米;1cm = = (96/2.54) pxcm is centimeters; 1cm==(96/2.54) px

pt 为点;1pt = = (96/72) pxpt is points; 1pt==(96/72) px

自动Auto
使行高由当前字体特征自动确定。Causes the line height to be determined automatically from the current font characteristics. 等效于 Double.NaN的属性值。Equivalent to a property value of Double.NaN.

依赖项属性信息Dependency Property Information

标识符字段Identifier field LineHeightProperty
元数据属性设置为 trueMetadata properties set to true AffectsMeasure, AffectsRender, InheritsAffectsMeasure, AffectsRender, Inherits

适用于

另请参阅