WPF TextBlock

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

例如:

    <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

转载于:https://www.cnblogs.com/dongxizhen/archive/2012/08/25/TextBlock.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值