TextBlock.FontSize 属性

定义

获取或设置 TextBlock 的顶级字号。Gets or sets the top-level font size for the TextBlock.

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

属性值

希望使用的字号,以与设备无关的像素为单位。The desired font size to use in device independent pixels). 默认值由 MessageFontSize 值确定。The default is determined by the MessageFontSize value.

属性

示例

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

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

注解

对子元素上的任何 FontSize 设置都将覆盖此顶级设置。Any FontSize settings on child elements override this top-level setting.

此依赖属性还具有附加的属性用法。This dependency property also has an attached property usage. XAMLXAML中,使用情况是 <object TextBlock.FontSize="value".../>的,其中object是包含在 TextBlock内的对象元素(通常是 flow 元素),是字符串格式值之一,如 XAML 值中所述。In XAMLXAML, the usage is <object TextBlock.FontSize="value".../>, where object is an object element (typically a flow element) contained within a TextBlock, and value is one of the string-format values as explained in XAML Values. 在代码中,GetFontSizeSetFontSize支持附加的属性用法。In code, the attached property usage is supported by GetFontSize and SetFontSize. 附加的属性用法并不常见,因为可以包含在 TextBlock 中的大多数元素都支持一个类似的非附加 FontSize 属性,TextBlock 用于呈现。The attached property usage is not common, because most elements that can be contained in a TextBlock support an analogous nonattached FontSize property, which the TextBlock uses for rendering.

XAML 属性用法XAML Attribute Usage

<object FontSize="double"/>  
- or -  
<object FontSize="qualifiedDouble"/>  

XAML 值XAML Values

doubledouble
Double

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

qualifiedDoublequalifiedDouble
如上所述的双精度值,后跟以下单元说明符之一: pxincm``ptA double value as described above, 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

依赖项属性信息Dependency Property Information

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

适用于