【问题标题】:xaml TextBlock to show text with partial bold font [duplicate]xaml TextBlock以部分粗体显示文本[重复]
【发布时间】:2022-01-20 03:42:44
【问题描述】:

我将从我的数据库中获取一个字符串并将其中的一部分(例如匹配关键字)设置为粗体。然后将其显示在 TextBlock 上。 例如。 “嗨,汤姆是谁?我需要找到他。”

我参考这个链接: https://social.msdn.microsoft.com/Forums/en-US/bb1f558c-a2dd-4977-85d7-8e0ce9631681/how-to-make-part-of-a-string-bold-in-c?forum=aspgettingstarted 将匹配的单词翻译成粗体:

private string FormatString(string wholeString, string boldPart)
{
    return Regex.Replace(wholeString, boldPart, @"<b>$0</b>", RegexOptions.IgnoreCase);
}

然后我得到这个格式化的新字符串“嗨,谁是 Tom?我需要找到他。”

但是在我把它放到 TextBlock 的 Text 属性之后,我得到了一些类似下面的错误。

【问题讨论】:

  • 为了动态设置这样的文本,您必须访问 TextBlock 的 Inlines 属性。
  • 重复标记链接指向已知文本的“静态”解决方案。但这不是多文本(动态)的解决方案,例如从数据库中获取。当您有动态文本时,重复链接将无法解决此问题。寻找我的答案,它将解决您设置为TextBlock 对象的任何文本的问题。所以,这不是一个重复的问题。

标签: c# wpf xaml


【解决方案1】:

您可以将文本拆分为多个Runs,并使用FontWeight 作为粗体文本。

<TextBlock>
    <Run Text="Who is "/>
    <Run FontWeight="Bold" Text="Tom"/>
    <Run Text="? I need to find him!"/>
</TextBlock>

或者

<TextBlock>Who is <Bold>Tom</Bold>? I need to find him!</TextBlock>

已编辑: 为了能够在单个TextBlock 中添加不同的格式,我想您可以订阅TextBlockTargetUpdated 事件并为&lt;b&gt;&lt;/b&gt; 之间的每个字符串添加Run

TargetUpdated 事件示例:

        private void MyText_TargetUpdated(object sender, DataTransferEventArgs e)
        {
            string text = myText.Text;

            if (text.Contains("<b>") && text.Contains("</b>"))
            {
                int nrOfB = text.Split("<b>", StringSplitOptions.None).Length - 1;
                myText.Text = "";
                string textAfter ="";
                for (int i = 0; i < nrOfB; i++)
                {
                    int startIndex = text.IndexOf("<b>");
                    int endIndex = text.IndexOf("</b>");
                    string textBefore = text.Substring(0, startIndex);
                    string textBolded = text.Substring(startIndex + 3, endIndex - (startIndex + 3));
                    textAfter = text.Substring(endIndex + 4);
                    Debug.WriteLine($"Text Before: {textBefore},\nTextBolded: {textBolded},\nTextAfter: {textAfter}");
                    myText.Inlines.Add(new Run(textBefore));
                    myText.Inlines.Add(new Bold(new Run(textBolded)));
                    text = textAfter;
                }
                myText.Inlines.Add(new Run(textAfter));

            }
        }

xaml 控件将是:

 <UserControl.Resources>
        <Style TargetType="local:Test"> 
            <Setter Property="MyText" Value="{Binding MyTextVM, Mode=TwoWay}"/>
        </Style>
    </UserControl.Resources>

<TextBlock x:Name="myText" Text="{Binding ElementName=TestV, Path=MyText, Mode=OneWay, NotifyOnTargetUpdated=True}" TargetUpdated="MyText_TargetUpdated"/>
local:Test - my View,
TestV - Name for the view,
MyText - DependencyProperty of my custom TextBlock class,
MyTextVM - property from ViewModel

此字符串:My Text is a &lt;b&gt;Bold&lt;/b&gt; text. &lt;b&gt;Second&lt;/b&gt; text bold is &lt;b&gt;Bolded&lt;/b&gt; too. 将创建此视觉效果:

【讨论】:

  • 嗨@Clemens。在我发布答案后,我对此更加努力,而且这个解决方案是相当硬编码的。我不认为是个好主意。现在我正在测试一些关注TargetUpdated 事件的内容,并将Textblock 的文本替换为多个Run,具体取决于文本中有多少&lt;b&gt;
  • 好的,我已经为动态字符串添加了很好的解决方案。查看编辑。
猜你喜欢
  • 1970-01-01
  • 2013-02-10
  • 1970-01-01
  • 2014-01-16
  • 2010-10-16
  • 2016-05-25
  • 2013-07-01
  • 1970-01-01
相关资源
最近更新 更多