WPF利用TextBlock实现查找结果高亮显示效果

 更新时间:2023年08月28日 10:02:45   作者:czwy  
在应用开发过程中,经常遇到这样的需求:通过关键字查找数据,把带有关键字的数据显示出来,同时在结果中高亮显示关键字,所以本文就来和大家介绍一下如何利用TextBlock实现查找结果高亮显示效果吧

前言

在应用开发过程中,经常遇到这样的需求:通过关键字查找数据,把带有关键字的数据显示出来,同时在结果中高亮显示关键字。在web开发中,只需在关键字上加一层标签,然后设置标签样式就可以轻松实现。

在WPF中显示文本内容通常采用TextBlock控件,也可以采用类似的方式,通过内联流内容元素Run达到同样的效果:

<TextBlock FontSize="20">
    <Run Text="Hel" /><Run Foreground="Red" Text="lo " /><Run Text="Word" />
</TextBlock>

需要注意的是每个Run之间不要换行,如果换行的话,每个Run之间会有间隙,看起来像增加了空格。

通过这种方式实现查找结果中高亮关键字,需要把查找结果拆分成三部分,然后绑定到Run元素的Text属性,或者在后台代码中使用TextBlockInlines属性添加Run元素

textBlock1.Inlines.Add(new Run("hel"));
textBlock1.Inlines.Add(new Run("lo ") { Foreground=new SolidColorBrush(Colors.Red)});
textBlock1.Inlines.Add(new Run("world"));

这种方法虽然可以达到效果,但显然与MVVM的思想不符。接下来本文介绍一种通过附加属性实现TextBlock中指定内容高亮。

技术要点与实现

通过TextEffectPositionStartPositionCount以及Foreground属性设置字符串中需要高亮内容的起始位置、长度以及高亮颜色。定义附加属性允许TextBlock设置需要高亮的内容位置以及颜色。

  • 首先定义类ColoredLettering(并不要求继承DependencyObject)。
  • ColoredLettering中注册自定义的附加属性,注册附加属性方式与注册依赖属性类似,不过附加属性是用DependencyProperty.RegisterAttached来注册。
  • 给附加属性注册属性值变化事件,事件处理逻辑中设置TextEffectPositionStartPositionCount以及Foreground实现内容高亮。
public class ColoredLettering
{
    public static void SetColorStart(TextBlock textElement, int value)
    {
        textElement.SetValue(ColorStartProperty, value);
    }
    public static int GetColorStart(TextBlock textElement)
    {
        return (int)textElement.GetValue(ColorStartProperty);
    }
    // Using a DependencyProperty as the backing store for ColorStart.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ColorStartProperty =
        DependencyProperty.RegisterAttached("ColorStart", typeof(int), typeof(ColoredLettering), new FrameworkPropertyMetadata(0, OnColorStartChanged));
    private static void OnColorStartChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBlock textBlock = d as TextBlock;
        if (textBlock != null)
        {
            if (e.NewValue == e.OldValue) return;
                if (e.NewValue is int)
                {
                    int count = GetColorLength(textBlock);
                    Brush brush = GetForeColor(textBlock);
                    if ((int)e.NewValue <= 0 || count <= 0 || brush == TextBlock.ForegroundProperty.DefaultMetadata.DefaultValue) return;
                    if (textBlock.TextEffects.Count != 0)
                    {
                        textBlock.TextEffects.Clear();
                    }
                    TextEffect textEffect = new TextEffect()
                    {
                        Foreground = brush,
                        PositionStart = (int)e.NewValue,
                        PositionCount = count
                    };
                    textBlock.TextEffects.Add(textEffect);
                }
        }
    }
    public static void SetColorLength(TextBlock textElement, int value)
    {
        textElement.SetValue(ColorLengthProperty, value);
    }
    public static int GetColorLength(TextBlock textElement)
    {
        return (int)textElement.GetValue(ColorLengthProperty);
    }
    // Using a DependencyProperty as the backing store for ColorStart.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ColorLengthProperty =
        DependencyProperty.RegisterAttached("ColorLength", typeof(int), typeof(ColoredLettering), new FrameworkPropertyMetadata(0, OnColorLengthChanged));
    private static void OnColorLengthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBlock textBlock = d as TextBlock;
            if (textBlock != null)
            {
                if (e.NewValue == e.OldValue) return;
                if (e.NewValue is int)
                {
                    int start = GetColorStart(textBlock);
                    Brush brush = GetForeColor(textBlock);
                    if ((int)e.NewValue <= 0 || start <= 0 || brush == TextBlock.ForegroundProperty.DefaultMetadata.DefaultValue) return;
                    if (textBlock.TextEffects.Count != 0)
                    {
                        textBlock.TextEffects.Clear();
                    }
                    TextEffect textEffect = new TextEffect()
                    {
                        Foreground = brush,
                        PositionStart = start,
                        PositionCount = (int)e.NewValue
                    };
                    textBlock.TextEffects.Add(textEffect);
                }
            }
    }
    public static void SetForeColor(TextBlock textElement, Brush value)
    {
        textElement.SetValue(ColorStartProperty, value);
    }
    public static Brush GetForeColor(TextBlock textElement)
    {
        return (Brush)textElement.GetValue(ForeColorProperty);
    }
    // Using a DependencyProperty as the backing store for ForeColor.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ForeColorProperty =
        DependencyProperty.RegisterAttached("ForeColor", typeof(Brush), typeof(ColoredLettering), new PropertyMetadata(TextBlock.ForegroundProperty.DefaultMetadata.DefaultValue, OnForeColorChanged));
    private static void OnForeColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBlock textBlock = d as TextBlock;
        if (textBlock != null)
        {
            if (e.NewValue == e.OldValue) return;
            if (e.NewValue is Brush)
            {
                int start = GetColorStart(textBlock);
                int count = GetColorLength(textBlock);
                if (start <= 0 || count <= 0) return;
                if (textBlock.TextEffects.Count != 0)
                {
                    textBlock.TextEffects.Clear();
                }
                TextEffect textEffect = new TextEffect()
                {
                    Foreground = (Brush)e.NewValue,
                    PositionStart = start,
                    PositionCount = count
                };
                textBlock.TextEffects.Add(textEffect);
            }
        }
    }
}

调用时只需在TextBlock指定需要高亮内容的开始位置,内容长度以及高亮颜色即可。

<TextBlock local:ColoredLettering.ColorLength="{Binding Count}"
           local:ColoredLettering.ColorStart="{Binding Start}"
           local:ColoredLettering.ForeColor="{Binding ForeColor}"
           FontSize="20"
           Text="Hello World" />

总结

本文介绍的方法只是高亮第一个匹配到的关键字,如果需要高亮匹配到的所有内容,只需要对附加属性进行改造,以支持传入一组位置和颜色信息。
最后分享一个可以解析一组有限的HTML标记并显示它们的WPF控件HtmlTextBlock ,通过这个控件也可以实现查找结果中高亮关键字,甚至支持指定内容触发事件做一些逻辑操作。

到此这篇关于WPF利用TextBlock实现查找结果高亮显示效果的文章就介绍到这了,更多相关WPF TextBlock内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#语句先后顺序对程序的结果有影响吗

    C#语句先后顺序对程序的结果有影响吗

    有朋友问我,C#中C#语句先后顺序影响程序的结果吗?告诉大家,答案是肯定的,绝对影响程序的结果,所以在程序中一定要注意C#语句的顺序
    2015-10-10
  • c# 防火墙添加/删除 特定端口的示例

    c# 防火墙添加/删除 特定端口的示例

    这篇文章主要介绍了c# 如何将特定端口加入防火墙中,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下
    2020-10-10
  • c#中合并excel表格的方法示例

    c#中合并excel表格的方法示例

    本篇文章主要介绍了c#中合并excel表格的方法,就是将excel表格结构一样的合并到一起,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-10-10
  • c#中如何去除字符串左边的0

    c#中如何去除字符串左边的0

    这篇文章主要介绍了c#中如何去除字符串左边的0问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • C#使用StructLayout特性来控制内存结构的操作代码

    C#使用StructLayout特性来控制内存结构的操作代码

    在C#中,内存布局对于性能优化和与非托管代码的互操作性至关重要,StructLayout特性允许开发者控制结构在内存中的布局方式,本文给大家介绍了C#使用StructLayout特性来控制内存结构的操作,需要的朋友可以参考下
    2024-10-10
  • C#正则表达式(Regex类)用法实例总结

    C#正则表达式(Regex类)用法实例总结

    正则表达式的主要作用是验证字符串的值是否满足一定的规则,在页面输入数据验证方面的应用比较多,下面这篇文章主要给大家介绍了关于C#正则表达式(Regex类)用法的相关资料,需要的朋友可以参考下
    2022-08-08
  • 关于C#操作文件路径(Directory)的常用静态方法详解

    关于C#操作文件路径(Directory)的常用静态方法详解

    这篇文章主要给大家介绍了关于C#操作文件路径(Directory)的常用静态方法,Directory类位于System.IO 命名空间,Directory类提供了在目录和子目录中进行创建移动和列举操作的静态方法,需要的朋友可以参考下
    2021-08-08
  • c# Base64编码和图片的互相转换代码

    c# Base64编码和图片的互相转换代码

    Base64编码在Web方面有很多应用,譬如在URL、电子邮件方面。网上有很多相关的资源用于提供Base64编码和其他编码的转换,.Net Framework也提供了现成的功能类(System.Convert)用于将二进制数据转换为Base64字符串
    2014-08-08
  • C#基础:Dispose()、Close()、Finalize()的区别详解

    C#基础:Dispose()、Close()、Finalize()的区别详解

    本篇文章是对c#中的Dispose()、Close()、Finalize()的区别进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Quartz.Net调度框架配置解析

    Quartz.Net调度框架配置解析

    这篇文章主要为大家详细介绍了Quartz.Net调度框架的配置方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07

最新评论