TextBlock.TextAlignment 属性

定义

获取或设置一个值,该值指示文本内容的水平对齐方式。Gets or sets a value that indicates the horizontal alignment of text content.

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

属性值

TextAlignment 值之一,它指定所需对齐方式。One of the TextAlignment values that specifies the desired alignment. 默认值为 LeftThe default is Left.

示例

下面的示例演示如何设置 TextBlock 元素的 TextAlignment 特性。The following example shows how to set the TextAlignment 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>

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

注解

此依赖属性还具有附加的属性用法。This dependency property also has an attached property usage. XAMLXAML中,使用情况是 <object TextBlock.TextAlignment="value".../>的,其中object是包含在 TextBlock内的对象元素(通常是 flow 元素),TextAlignment 枚举的字符串值。In XAMLXAML, the usage is <object TextBlock.TextAlignment="value".../>, where object is an object element (typically a flow element) contained within a TextBlock, and value is a string value of the TextAlignment enumeration. 在代码中,GetTextAlignmentSetTextAlignment支持附加的属性用法。In code, the attached property usage is supported by GetTextAlignment and SetTextAlignment. 附加的属性用法并不常见。The attached property usage is not common.

依赖项属性信息Dependency Property Information

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

适用于

另请参阅