如何在 WPF 中创建 TextBlock 控件并设置字体加粗

1. 创建 TextBlock 控件

// 创建一个新的 TextBlock 控件
var titleTextBlock = new TextBlock() {
    Text = 'caption',
    FontSize = 18,
    Foreground = Brushes.Blue,
};

// 将 TextBlock 添加到 StackPanel 中
stackPanel.Children.Add(titleTextBlock);

2. 设置字体加粗

可以通过设置 TextBlock 的 FontWeight 属性来实现字体加粗,例如:

titleTextBlock.FontWeight = FontWeights.Bold;

示例代码

// 创建一个新的 TextBlock 控件
var titleTextBlock = new TextBlock() {
    Text = '标题', // 设置文本内容
    FontSize = 18, // 设置字体大小
    Foreground = Brushes.Blue, // 设置字体颜色
    FontWeight = FontWeights.Bold // 设置字体加粗
};

// 将 TextBlock 添加到 StackPanel 中
stackPanel.Children.Add(titleTextBlock);

注意:

  • caption 是一个占位符,请替换成您要显示的实际文本内容。
  • stackPanel 是一个 StackPanel 对象,请根据您的实际代码进行调整。
  • FontWeights.Bold 是一个枚举值,表示字体加粗。

更多信息:

WPF 创建 TextBlock 控件并设置字体加粗

原文地址: http://www.cveoy.top/t/topic/j7st 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录