You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WPF:如何通过绑定与转换器实现TextBlock指定文字加粗?

解决WPF TextBlock绑定特定单词加粗的问题

问题根源

  1. 绑定Inlines报错:InlineCollection不是DependencyObject,而MultiBinding只能绑定到DependencyProperty,因此直接绑定TextBlock.Inlines会触发错误。
  2. 绑定Text显示为空:TextBlock.Text属性接收字符串类型,但转换器返回的是InlineCollection,类型不匹配导致无法渲染内容。

可行方案:使用附加属性实现

通过自定义附加属性,将MultiBinding绑定到附加属性(属于DependencyProperty),在属性变更时直接操作TextBlock的Inlines集合,生成带加粗格式的内容。

1. 实现附加属性类

using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

public static class TextBlockExtensions
{
    // 定义附加属性,接收包含原文本和目标单词的对象数组
    public static readonly DependencyProperty FormattedTextProperty =
        DependencyProperty.RegisterAttached(
            "FormattedText",
            typeof(object[]),
            typeof(TextBlockExtensions),
            new PropertyMetadata(null, OnFormattedTextChanged));

    public static object[] GetFormattedText(DependencyObject obj)
    {
        return (object[])obj.GetValue(FormattedTextProperty);
    }

    public static void SetFormattedText(DependencyObject obj, object[] value)
    {
        obj.SetValue(FormattedTextProperty, value);
    }

    // 属性变更时处理文本格式
    private static void OnFormattedTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is not TextBlock textBlock) return;
        var values = e.NewValue as object[];
        if (values?.Length != 2) return;

        var line = values[0] as string;
        var targetWord = values[1] as string;

        textBlock.Inlines.Clear();
        if (string.IsNullOrEmpty(line)) return;

        // 查找目标单词位置
        var index = line.IndexOf(targetWord, StringComparison.InvariantCultureIgnoreCase);
        if (index < 0)
        {
            textBlock.Inlines.Add(new Run(line));
            return;
        }

        // 添加前置非加粗文本
        if (index > 0)
        {
            textBlock.Inlines.Add(new Run(line.Substring(0, index)));
        }

        // 添加加粗的目标单词
        textBlock.Inlines.Add(new Bold(new Run(line.Substring(index, targetWord.Length))));

        // 添加后置非加粗文本
        var remainingTextLength = line.Length - index - targetWord.Length;
        if (remainingTextLength > 0)
        {
            textBlock.Inlines.Add(new Run(line.Substring(index + targetWord.Length)));
        }
    }
}

2. XAML中使用附加属性

<Window xmlns:local="clr-namespace:YourNamespace">
    <TextBlock>
        <local:TextBlockExtensions.FormattedText>
            <MultiBinding>
                <!-- 绑定原文本数据源 -->
                <Binding Path="OriginalText" />
                <!-- 绑定需要加粗的目标单词 -->
                <Binding Path="TargetBoldWord" />
            </MultiBinding>
        </local:TextBlockExtensions.FormattedText>
    </TextBlock>
</Window>

方案优势

  • 规避了MultiBinding无法直接绑定集合的限制,利用附加属性的DependencyProperty特性实现绑定。
  • 直接操作TextBlock.Inlines,逻辑清晰,无需额外转换器(原转换器的逻辑已整合到附加属性的回调中)。
  • 支持大小写不敏感的单词匹配,与原需求一致。

内容的提问来源于stack exchange,提问作者Ryan Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 12:30:54