TextBlock.FontWeight 属性

定义

获取或设置 TextBlock 的顶级字体粗细。Gets or sets the top-level font weight for the TextBlock.

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

属性值

FontWeights 类的一个成员,指定所需的字体粗细。A member of the FontWeights class specifying the desired font weight. 默认值由 MessageFontWeight 值确定。The default is determined by the MessageFontWeight value.

示例

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

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

注解

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

有关字体粗细的有效值的列表,请参阅 FontWeights 类。For a list of valid values of font weights, see the FontWeights class.

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

依赖项属性信息Dependency Property Information

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

适用于