上善若水

水善利万物而不争
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WPF TextBlock

Posted on 2012-08-25 08:44  董锡振  阅读(1184)  评论(0编辑  收藏  举报

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