【问题标题】:Formatting text in a TextBlock格式化 TextBlock 中的文本
【发布时间】:2011-07-12 21:26:28
【问题描述】:

如何在我的 WPF 应用程序中实现 TextBlock 控件内的文本格式设置?

例如:我想将某些单词以粗体显示,将其他单词以斜体显示,并以不同的颜色显示,例如以下示例:

我的问题背后的原因是这个实际问题:

lblcolorfrom.Content = "Colour From: " + colourChange.ElementAt(3).Value.ToUpper();

我希望字符串的第二部分加粗,并且我知道我可以使用两个控件(标签、文本块等),但我不想这样做,因为已经有大量控件在使用。

【问题讨论】:

    标签: c# wpf wpf-controls


    【解决方案1】:

    你需要使用Inlines:

    <TextBlock.Inlines>
        <Run FontWeight="Bold" FontSize="14" Text="This is WPF TextBlock Example. " />
        <Run FontStyle="Italic" Foreground="Red" Text="This is red text. " />
    </TextBlock.Inlines>
    

    带绑定:

    <TextBlock.Inlines>
        <Run FontWeight="Bold" FontSize="14" Text="{Binding BoldText}" />
        <Run FontStyle="Italic" Foreground="Red" Text="{Binding ItalicText}" />
    </TextBlock.Inlines>
    

    你也可以绑定其他属性:

    <TextBlock.Inlines>
        <Run FontWeight="{Binding Weight}"
             FontSize="{Binding Size}"
             Text="{Binding LineOne}" />
        <Run FontStyle="{Binding Style}"
             Foreground="Binding Colour}"
             Text="{Binding LineTwo}" />
    </TextBlock.Inlines>
    

    如果您将粗体作为布尔值(例如),则可以通过转换器进行绑定。

    【讨论】:

      【解决方案2】:

      您可以在 XAML 中轻松做到这一点:

      <TextBlock>
        Hello <Bold>my</Bold> faithful <Underline>computer</Underline>.<Italic>You rock!</Italic>
      </TextBlock>
      

      【讨论】:

      • 太棒了!我不知道 XAML 支持这样的结构。
      • 这个支持绑定吗?
      • @ArsenMkrt 怎么样:
      • @Aetherix 我无法让它工作。我从qqbenq使用这个:每月还款£
      • @Arsen 不,它不支持绑定。
      【解决方案3】:

      有各种Inline 元素可以帮助您,对于最简单的格式化选项,您可以使用BoldItalicUnderline

      <TextBlock>
          Sample text with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> words.
      </TextBlock>
      

      我认为值得注意的是,这些元素实际上只是具有各种属性集的Span 元素的简写(即:对于BoldFontWeight 属性设置为FontWeights.Bold)。

      这给我们带来了下一个选项:前面提到的Span 元素。

      你可以用这个元素达到和上面一样的效果,但你被赋予了更多的可能性;您可以设置(除其他外)ForegroundBackground 属性:

      <TextBlock>
          Sample text with <Span FontWeight="Bold">bold</Span>, <Span FontStyle="Italic">italic</Span> and <Span TextDecorations="Underline">underlined</Span> words. <Span Foreground="Blue">Coloring</Span> <Span Foreground="Red">is</Span> <Span Background="Cyan">also</Span> <Span Foreground="Silver">possible</Span>.
      </TextBlock>
      

      Span 元素还可以包含其他元素,如下所示:

      <TextBlock>
          <Span FontStyle="Italic">Italic <Span Background="Yellow">text</Span> with some <Span Foreground="Blue">coloring</Span>.</Span>
      </TextBlock>
      

      还有一个元素,和Span很相似,叫做RunRun 不能包含其他内联元素,而 Span 可以,但您可以轻松地将变量绑定到 RunText 属性:

      <TextBlock>
          Username: <Run FontWeight="Bold" Text="{Binding UserName}"/>
      </TextBlock>
      

      此外,如果您愿意,您可以从代码隐藏中进行整个格式化:

      TextBlock tb = new TextBlock();
      tb.Inlines.Add("Sample text with ");
      tb.Inlines.Add(new Run("bold") { FontWeight = FontWeights.Bold });
      tb.Inlines.Add(", ");
      tb.Inlines.Add(new Run("italic ") { FontStyle = FontStyles.Italic });
      tb.Inlines.Add("and ");
      tb.Inlines.Add(new Run("underlined") { TextDecorations = TextDecorations.Underline });
      tb.Inlines.Add("words.");
      

      【讨论】:

        【解决方案4】:

        从 Charles Petzolds Bool Application = Code + markup 查看这个示例

        //----------------------------------------------
        // FormatTheText.cs (c) 2006 by Charles Petzold
        //----------------------------------------------
        using System;
        using System.Windows;
        using System.Windows.Controls;
        using System.Windows.Input;
        using System.Windows.Media;
        using System.Windows.Documents;
        
        namespace Petzold.FormatTheText
        {
            class FormatTheText : Window
            {
                [STAThread]
                public static void Main()
                {
                    Application app = new Application();
                    app.Run(new FormatTheText());
                }
                public FormatTheText()
                {
                    Title = "Format the Text";
        
                    TextBlock txt = new TextBlock();
                    txt.FontSize = 32; // 24 points
                    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;
        
                    Content = txt;
                }
            }
        }
        

        【讨论】:

          【解决方案5】:

          一个很好的网站,有很好的解释:

          http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/

          这里作者为您提供了您正在寻找的好例子!总体而言,该网站非常适合研究材料,而且它涵盖了您在 WPF 中拥有的大量选项

          编辑

          有不同的方法来格式化文本。对于基本格式(我认为最简单的):

              <TextBlock Margin="10" TextWrapping="Wrap">
                              TextBlock with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> text.
              </TextBlock>
          

          示例 1 显示了带有 粗体 斜体和下划线文本的基本格式。

          以下包括 SPAN 方法,您可以使用此方法突出显示文本:

             <TextBlock Margin="10" TextWrapping="Wrap">
                              This <Span FontWeight="Bold">is</Span> a
                              <Span Background="Silver" Foreground="Maroon">TextBlock</Span>
                              with <Span TextDecorations="Underline">several</Span>
                              <Span FontStyle="Italic">Span</Span> elements,
                              <Span Foreground="Blue">
                                      using a <Bold>variety</Bold> of <Italic>styles</Italic>
                              </Span>.
             </TextBlock>
          

          示例 2 显示了 span 函数及其不同的可能性。

          查看网站了解详细说明!

          Examples

          【讨论】:

          • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
          • @Mogsdad 编辑了帖子,因此它显示了代码示例
          • @RichardSlater 编辑了帖子,因此它显示了代码示例
          【解决方案6】:

          这是我的解决方案....

              <TextBlock TextWrapping="Wrap" Style="{DynamicResource InstructionStyle}"> 
                  <Run Text="This wizard will take you through the purge process in the correct order." FontWeight="Bold"></Run>
                  <LineBreak></LineBreak>
                  <Run Text="To Begin, select" FontStyle="Italic"></Run>
                  <Run x:Name="InstructionSection" Text="'REPLACED AT RUNTIME'" FontWeight="Bold"></Run>
                  <Run Text="from the menu." FontStyle="Italic"></Run>
              </TextBlock>
          

          我正在学习...所以如果有人对上述解决方案有任何想法,请分享! :)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-12-20
            • 1970-01-01
            • 1970-01-01
            • 2020-01-09
            • 2015-02-13
            • 1970-01-01
            相关资源
            最近更新 更多