TextBlock.FontFamily 属性


获取或设置 TextBlock 的首选最常用字体系列。Gets or sets the preferred top-level font family for the TextBlock.

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


一个 FontFamily 对象,指定首选字体系列,或者指定一个主要首选字体系列并附带一个或多个备用字体系列。A FontFamily object specifying the preferred font family, or a primary preferred font family with one or more fallback font families. 默认值为由 MessageFontFamily 值确定的字体。The default is the font determined by the MessageFontFamily value.



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

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


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

指定多个系列后,第二个和后续的字体系列作为备用系列,在主要字体系列不可用或不适用的情况下使用。When multiple families are specified, the second and subsequent font families serve as fallback families to be used in cases where the primary font family is unavailable or not applicable.

此属性仅指定一个首选项。This property specifies a preference only. 如果指定的字体系列不可用,则 TextBlock 会以静默方式回退到 MessageFontFamily 值确定的字体。If the specified font family is not available, the TextBlock silently falls back to the font determined by the MessageFontFamily value.

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

XAML 属性用法XAML Attribute Usage

<object FontFamily="fontFamilyName"/>  
- or -  
<object FontFamily="fontFamilyNamesList"/>  
- or -  
<object FontFamily="fontFamilyFolderReference"/>  
- or -  
<object FontFamily="fontFamilyUriReference"/>  


指定字体系列名称的字符串。A string specifying a font family name. 例如,"Arial""Century Gothic"For example, "Arial" or "Century Gothic".

指定多个字体系列名称的字符串,每个名称之间用逗号分隔(逗号后面的任何空白都将被忽略)。A string specifying multiple font family names, each separated by a comma (any white space following a comma is ignored). 指定的第一个字体系列用作主要字体系列;在主要字体系列不可用或不适用的情况下,后续字体系列用作备用系列。The first font family specified serves as the primary font family; subsequent font families serve as fallback families to be used in cases where the primary font family is unavailable or not applicable. 例如,"Arial, Century Gothic" 将 Arial 字体系列指定为 "Arial" 作为 "备用字体系列"。For example, "Arial, Century Gothic" specifies Arial as the primary font family, with Century Gothic as the fallback font family.

指定包含字体的文件夹以及字体系列名称的字符串。A string specifying a folder containing the font, along with a font family name. 文件夹和字体系列名称之间用 # 字符进行分隔。The folder and font family name are delimited by a # character. 文件夹引用可以是绝对的,也可以是相对的。The folder reference may be absolute, or relative. 例如 "Custom Fonts\#My Custom Font"For example, "Custom Fonts\#My Custom Font".

指定字体的统一资源标识符(URI)和字体系列名称的字符串。A string specifying a uniform resource identifier (URI) for the font, along with a font family name. URI 和字体系列名称由 # 字符分隔。The URI and font family name are delimited by a # character. 例如 "http://MyFontServer/Fonts/#My Custom Font"For example, "http://MyFontServer/Fonts/#My Custom Font".

依赖项属性信息Dependency Property Information

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