上善若水

水善利万物而不争
随笔 - 175, 文章 - 0, 评论 - 10, 阅读 - 14万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

WPF TextBlock

Posted on   董锡振  阅读(1203)  评论(0编辑  收藏  举报

TextBlock模型实际上指的就是System.Windows.Controls.TextBlock类,它是一个用于显示少量流内容的轻量控件。其中包含一个InLines属性,支持 Inline 流内容元素的承载和显示。 支持的元素包括 AnchoredBlock、Bold(粗体字符串)、Hyperlink(超链接,在浏览器支持的模式下有效)、InlineUIContainer(承载其他控件的容器)、Italic(斜体字符串)、LineBreak(换行符)、Run(普通字符串)、Span(可以设置字体、颜色等的Span) 和 Underline(下划线)。

例如:

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<StackPanel Orientation="Horizontal">
        <Border BorderThickness="2" Margin="5" BorderBrush="Black">
            <TextBlock Margin="5" TextWrapping="WrapWithOverflow">
                <TextBlock.Inlines>
                    <Bold>
                       <Run>BlockText 控件XAML示例</Run>
                    </Bold>
                    <LineBreak/>
                    <Run>TextBlock支持以下的几种流显示样式:</Run>
                   <LineBreak/>
                   <Bold>粗体(Bold)</Bold>
                   <LineBreak/>
                   <Italic>斜体(Italic)</Italic>
                   <LineBreak/>
                  <Underline>下划线(Underline)</Underline>
                   <LineBreak/>
                   <Hyperlink NavigateUri=http://www.microsof.com>
                      超级链接</Hyperlink>
                   <LineBreak/>
                   <Span Foreground="Red" FontSize="18">Span设置字体、颜色等</Span>
                   <LineBreak />
                  <InlineUIContainer>
                       <StackPanel Background="AntiqueWhite" Margin="5">
                           <TextBlock>Inline UI 容器</TextBlock>
                           <Button Content="按钮" Width="80" />
                       </StackPanel>
                   </InlineUIContainer>
               </TextBlock.Inlines>
           </TextBlock>
       </Border>
       <Border BorderThickness="2" Margin="5" BorderBrush="Black">
          <TextBlock Margin="5" TextWrapping="WrapWithOverflow" x:Name="textBlock">
               <TextBlock.Inlines>
                   <Bold>
                       <Run x:Name="title"></Run>
                   </Bold>
                   <LineBreak x:Name="line"/>
                   <InlineUIContainer x:Name="container">
                       <StackPanel Background="AntiqueWhite" Margin="5" x:Name="panel">
                       </StackPanel>
                   </InlineUIContainer>
               </TextBlock.Inlines>
           </TextBlock>
       </Border>
   </StackPanel>
使用代码操作Inlines:
           TextBlock myTextBlock = new TextBlock();
            myGrid.Children.Add(myTextBlock);
            myTextBlock.Inlines.Add("TextBlock的使用:");
            Italic myItalic = new Italic(new Run("(如Width Hight等)"));
            myItalic.FontSize = 24;
            myItalic.Foreground = Brushes.Purple;
            myTextBlock.Inlines.Add(myItalic);
            myTextBlock.Inlines.Add("是很奇怪的,它不是普通的像素,这个单位被称为与设备无关的单位");
            myTextBlock.TextWrapping = TextWrapping.WrapWithOverflow;
            Bold myBold = new Bold(new Italic(new Run("(Device-independent unit)")));
            myTextBlock.Inlines.Add(myBold);
            myTextBlock.HorizontalAlignment = HorizontalAlignment.Stretch;
            myTextBlock.Inlines.Add(new LineBreak());
            Bold myBold1 = new Bold(new Run("粗体"));
            myTextBlock.Inlines.Add(myBold1);
            myTextBlock.Inlines.Add(new LineBreak());
            Underline myUnderline = new Underline(new Run("下划线"));
            myTextBlock.Inlines.Add(myUnderline);
            myTextBlock.Inlines.Add(new LineBreak());
            Hyperlink myHyperlink=new Hyperlink(new Run("百度"));
            myHyperlink.NavigateUri = new Uri("http://www.baidu.com");
            myTextBlock.Inlines.Add(myHyperlink);
            myTextBlock.Inlines.Add(new LineBreak());
            Span mySpan = new Span(new Run("Span设置字体、颜色等"));
            mySpan.Foreground = Brushes.Red;//或者改为:mySpan.Foreground=new SolidColorBrush(Colors.Red);
            myTextBlock.Inlines.Add(mySpan);
            myTextBlock.Inlines.Add(new LineBreak());
            InlineUIContainer myInlineUIContainer = new InlineUIContainer();
             myTextBlock.Inlines.Add(myInlineUIContainer);
            StackPanel myStackPanel=new StackPanel();
            myInlineUIContainer.Child = myStackPanel;
            Button myButton = new Button();
            myButton.Content = "lucky";
            myStackPanel.Children.Add(myButton);

  

 

 

 转自:http://blog.csdn.net/zhangjiyehandsom/article/details/5498845

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示