WPF - TextBlock - 以编程方式格式化文本 [英] WPF - TextBlock - Format Text Programmatically

查看:36
本文介绍了WPF - TextBlock - 以编程方式格式化文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 TextBlock 对象中,您可以像这样设置 XAML 中的文本格式:

In the TextBlock object you can format the text in the XAML like this:

<TextBlock>
    <Bold>bold text</Bold> random non bold next
</TextBlock>

如何以编程方式处理粗体"标签?

How do you do the "Bold" tags programmatically?

我尝试将它们放在 text 属性中,然后它只是将它们打印出来(标签被打印为文本).

I tried just putting them in the text property and it just printed them out (the tags were printed as text).

推荐答案

Visual Basic 版本:

Visual Basic Version:

Dim tb As New TextBlock

Dim b As New Bold
b.Inlines.Add(New Run("bold text"))

tb.Inlines.Add(b)
tb.Inlines.Add(New Run("random non bold text"))

C# 版本:

TextBlock tb = new TextBlock();
var bold = new Bold(new Run("Bold Text"));
tb.Inlines.Add(bold);

var normal = new Run("Normal Text"));
tb.Inlines.Add(normal);

这篇关于WPF - TextBlock - 以编程方式格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆