TextBlock.Padding Property


获取或设置一个值,该值指示内容区域的边界之间填充空间的宽度,以及由 TextBlock 显示的内容。Gets or sets a value that indicates the thickness of padding space between the boundaries of the content area, and the content displayed by a TextBlock.

 property System::Windows::Thickness Padding { System::Windows::Thickness get(); void set(System::Windows::Thickness value); };
public System.Windows.Thickness Padding { get; set; }
member this.Padding : System.Windows.Thickness with get, set
Public Property Padding As Thickness

Property Value


一个 Thickness 结构,它指定要应用的边距量(以与设备无关的像素为单位)。A Thickness structure specifying the amount of padding to apply, in device independent pixels. 默认为 NaNThe default is NaN.


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

  FontFamily="Century Gothic"
  <Run Background="LightGreen">Text run 1.</Run>
  <LineBreak/><Run Background="LightBlue">Text run 2.</Run>
  <LineBreak/><Run Background="LightYellow">Text run 3.</Run>

下面的示例演示如何以编程方式设置 Padding 属性。The following example shows how to set the Padding 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


Padding 可以在所有方向(Padding="10")中以一致的方式来描述,也可以作为四个不同的值(分别表示左、上、右和下填充)(Padding="5,0,10,20")。Padding can be described as uniform in all directions (Padding="10"), or as four distinct values that represent left, top, right, and bottom padding independently (Padding="5,0,10,20").

如果指定的空白宽度超出了相应的内容区域尺寸(例如,左边距和右边距的宽度超过内容区域宽度),则填充的宽度将按比例缩小,使其不大于相关内容区域维度。If a specified padding thickness exceeds the corresponding content area dimension (for example, the sum of the left and right padding widths exceeds the content area width), the thickness of the padding is proportionally reduced to be no greater than the relevant content area dimension.

XAML 特性用法XAML Attribute Usage

<object Padding="uniformThickness"/>  
- or -  
<object Padding="independentThickness"/>  
- or -  
<object Padding="qualifiedUniformThickness"/>  
- or -  
<object Padding="qualifiedIndependentThickness"/>  


要统一应用于所有四个厚度尺寸的单个 Double 值的字符串表示形式。String representation of a single Double value to apply uniformly to all four thickness dimensions. 例如,值 "10" 等效于 "10,10,10,10"的值。For example, a value of "10" is equivalent to a value of "10,10,10,10". 非限定值以与设备无关的像素度量。An unqualified value is measured in device independent pixels. 字符串不需要显式包含小数点。Strings need not explicitly include decimal points.

四个有序 Double 值的字符串表示形式,这些值对应于左、上、右和下(按此顺序)的独立厚度尺寸。String representation of four ordered Double values corresponding to independent thickness dimensions for left, top, right, and bottom, in this order. 这四个值必须用逗号分隔;不允许使用空格。The four values must be separated with commas; spaces are not allowed. 例如,"5,10,15,20" 将在内容左侧填充5个像素,在内容右侧填充10个像素,在内容右侧填充15个像素,在内容的下方填充20个像素。For example, "5,10,15,20" results in 5 pixels of padding to the left of content, 10 pixels of padding above content, 15 pixels of padding to the right of content, and 20 pixels of padding below the content.

UniformThickness描述的值后跟以下单元说明符之一: pxinA value described by uniformThickness followed by one of the following unit specifiers: px, in.

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

例如,"1in" 在所有方向上都提供1英寸的统一填充。For example, "1in" provides uniform padding of 1 inch in all directions.

IndependentThickness描述的值,每个独立值后跟以下单元说明符之一: pxinA value described by independentThickness, with each independent value followed by one of the following unit specifiers: px, in.

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

例如,"1.5in,0.8in,1.5in,0.8in"For example, "1.5in,0.8in,1.5in,0.8in". 可以在一个或多个值中混合或省略单元说明符。Unit specifiers may be mixed or omitted from one or more values.

依赖项属性信息Dependency Property Information

标识符字段Identifier field PaddingProperty
元数据属性设置为 trueMetadata properties set to true AffectsMeasure

Applies to