WPF之格式化文本

本文介绍了如何在WPF中实现文本格式化,通过在Window_Loaded事件中设置TextBlock对象的FontSize、Inlines属性,创建不同样式的文本,如斜体、粗体和加粗斜体,同时展示了TextBlock类及其继承关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前台:默认即可
<Window x:Class="FormatText.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="FormatText" Height="300" Width="300" Loaded="Window_Loaded">
    <Grid>
    </Grid>
</Window>
后台:Window_Loaded(object sender,RoutedEventArgs e)事件中代码
 	   Title = "Format the Text";
            TextBlock txt = new TextBlock();
            txt.FontSize = 24;              // 24
            txt.Inlines.Add("This is some ");
            txt.Inlines.Add(new Italic(new Run("italic")));
            txt.Inlines.Add(" text, and this is some ");
            txt.Inlines.Add(new Bold(new Run("bold")));
            txt.Inlines.Add(" text, and let's cap it off with some ");
            txt.Inlines.Add(new Bold(new Italic(new Run("bold italic"))));
            txt.Inlines.Add(" text.");
            txt.TextWrapping = TextWrapping.Wrap;
            this.Content = txt;
TextBlock 类包含在System.Windows.Controls 命名空间内,但Inlines属于System.Windows.Documents
命名空间。具体的继承关系如下:
Object

    DispatcherObject (abstract)

           DependencyObject

                 ContentElement

                        FrameworkContentElement

                               TextElement (abstract)

                               Inline (abstract)

                                      Run

                                      Span

                                            Bold

                                            Hyperlink

                                            Italic

                                            Underline
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值