【问题标题】:Make a Part of Text Bold inside TextBlock在 TextBlock 内将部分文本加粗
【发布时间】:2014-01-16 04:05:33
【问题描述】:

我知道我们可以在 XAML 中使用 <Run> 来实现我的要求:

<TextBlock.Inlines>
    <Run Text="This is" />
    <Run FontWeight="Bold" Text="Bold Text." />
</TextBlock.Inlines>

我也可以在后面的代码中这样做:

TextBlock.Inlines.Add(new Run("This is"));
TextBlock.Inlines.Add(new Bold(new Run("Bold Text.")));

但我的问题有所不同:

假设我的数据库中有以下文本:

This is <b>Bold Text</b>.

现在,我的 Textblock 已绑定到数据库中包含上述文本的字段。

我想要text between &lt;b&gt; and &lt;/b&gt; to be bold。我怎样才能做到这一点?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    如果要显示 HTML,请使用 Webbrowser 控件。

    <WebBrowser Name="myWebBrowser"/>
    

    在您的代码中,像这样传递您的文本:

    myWebBrowser.NavigateToString(myHTMLString);
    

    如果不是,加粗是唯一要做的,不能嵌套,可以这样:

    string s = "<b>This</b> is <b>bold</b> text <b>bold</b> again."; // Sample text
    var parts = s.Split(new []{"<b>", "</b>"}, StringSplitOptions.None);
    bool isbold = false; // Start in normal mode
    foreach (var part in parts)
    {
         if (isbold)
            myTextBlock.Inlines.Add(new Bold(new Run(part)));
         else
            myTextBlock.Inlines.Add(new Run(part));
    
         isbold = !isbold; // toggle between bold and not bold
    }
    

    【讨论】:

    • 不,我不想使用 WebBrowser Control。我也不想显示 HTML。 &lt;b&gt; and &lt;/b&gt; 之间的文本就是一个例子。它可以是两颗星之间的文本,而不是&lt;b&gt; and &lt;/b&gt;,也可以是任何东西。我只想将该文本用作文本块的文本。
    • 你的文本中可以有任意数量的粗体部分?
    • 噢,我没有注意到你的更新。谢谢你。发现它比第三个答案更有帮助。
    • @MichalHainc:猜猜我为什么要这个数字......它可能会变得很慢,有数千人。也许它甚至会在 32 位进程中给出 OutOfMemoryException。但在我知道需求是什么之前,我不喜欢针对速度或内存进行优化。
    【解决方案2】:

    您似乎想用&lt;Bold&gt; 替换您的自定义格式 - 请参阅TextBlock 了解更多信息。文章示例:

    <TextBlock Name="textBlock1" TextWrapping="Wrap">
      <Bold>TextBlock</Bold> is designed to be <Italic>lightweight</Italic>,
      and is geared specifically at integrating <Italic>small</Italic> portions
      of flow content into a UI.
    </TextBlock>
    

    一种方法是重新格式化字符串以匹配 TextBlock 的预期。

    如果您有 HTML 输入 - 首先使用 HtmlAgilityPack 解析文本,然后遍历结果元素并使用 b-elements 构造字符串,替换为包裹 &lt;Bold&gt; 的文本并类似于其他格式。

    如果已知数据库内容只有有效的开始/结束对(不是随机 HTML),您甚至可以使用基本的 String.Replace : text = text.Replace( "", "")`。

    如果您有自己的自定义格式(如*boldtext*),则需要为此发明自定义解析器。

    【讨论】:

      【解决方案3】:

      您可以订阅TargetUpdated事件:

       void textBlock_TargetUpdated(object sender, DataTransferEventArgs e)
       {
              string text = textBlock.Text;
      
              if (text.Contains("<b>"))
              {
                  textBlock.Text = "";
                  int startIndex = text.IndexOf("<b>");
                  int endIndex = text.IndexOf("</b>");
                  textBlock.Inlines.Add(new Run(text.Substring(0, startIndex)));
                  textBlock.Inlines.Add(new Bold(new Run(text.Substring(startIndex + 3, endIndex - (startIndex + 3)))));
                  textBlock.Inlines.Add(new Run(text.Substring(endIndex + 4)));
              }
          }
      

      和用于TextBlock 的 XAML:

      <TextBlock x:Name="textBlock" Text="{Binding NotifyOnTargetUpdated=True}"></TextBlock>
      

      【讨论】:

      • 这是一个非常具体的解决方案,仅在存在 1 的情况下才有效
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2012-09-02
      • 2017-10-02
      • 2017-01-23
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      相关资源
      最近更新 更多