文笔不好,就长话短说,就是想实现这样的效果,比如在成都二环路南一段一号附一号凤舞九天网吧 ,搜索 二环路 九天网吧 然后结果中高亮显示。

         <local:TextBlockHighLight TextSource="成都二环路南一段一号附一号凤舞九天网吧" SearchText="二环路 九天网吧 "  FontSize="12" FontFamily="Comic Sans MS"  Margin="10"/>

代码如下:

    public partial class TextBlockHighLight : UserControl
{
public TextBlockHighLight()
{
InitializeComponent();
this.LayoutRoot.Children.Add(_txtBlock);
}
public string TextSource
{
get { return (string)GetValue(TextSourceProperty); }
set { SetValue(TextSourceProperty, value); }
}
private TextBlock _txtBlock = new TextBlock() { TextWrapping = TextWrapping.Wrap };
// Using a DependencyProperty as the backing store for TextSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextSourceProperty =
DependencyProperty.Register("TextSource", typeof(string), typeof(TextBlockHighLight), new PropertyMetadata(string.Empty, (o, e) =>
{
var _this = o as TextBlockHighLight;
if (string.IsNullOrWhiteSpace(e.NewValue.ToString()))
{
_this._txtBlock.Text = string.Empty;
}
else
{
_this._txtBlock.Text = e.NewValue.ToString();
}
})); public string SearchText
{
get { return (string)GetValue(SearchTextProperty); }
set { SetValue(SearchTextProperty, value); }
} // Using a DependencyProperty as the backing store for SearchText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SearchTextProperty =
DependencyProperty.Register("SearchText", typeof(string), typeof(TextBlockHighLight), new PropertyMetadata(string.Empty, (o, e) =>
{
var _this = o as TextBlockHighLight;
///如果字符都不为空
if ((!string.IsNullOrWhiteSpace(e.NewValue.ToString()))
&& (!string.IsNullOrWhiteSpace(_this._txtBlock.Text)))
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">");
sb.Append(" <TextBlock.Inlines>");
sb.Append("<Run>"); var strs = e.NewValue.ToString().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
if (strs.Length > )
{
///移除相同的 比如二环路 二环 那么会移除二环
for (int i = ; i < strs.Length; i++)
{
for (int j = i + ; j < strs.Length; j++)
{
if (strs[j].Contains(strs[i]))
{
///无效的就设为空字符串
strs[i] = string.Empty;
break;
}
}
}
} ///去除空字符串
strs = strs.Where(c => !string.IsNullOrEmpty(c)).ToArray(); var tmpSB = new System.Text.StringBuilder(); foreach (var item in strs)
{
if (tmpSB.Length == )
{
///设置要高亮显示的样式 ,这里是测试就写死了
tmpSB.Append(_this.TextSource.Replace(item, "</Run><Run Text=\"" + item + "\" Foreground=\"Red\" FontWeight=\"Bold\"></Run> <Run>"));
}
else
{
tmpSB.Replace(item, "</Run><Run Text=\"" + item + "\" Foreground=\"Red\" FontWeight=\"Bold\"></Run> <Run>");
}
}
sb.Append(tmpSB.ToString());
sb.Append("</Run>");
sb.Append("</TextBlock.Inlines>");
sb.Append("</TextBlock>"); var txt = System.Windows.Markup.XamlReader.Load(sb.ToString()) as TextBlock; List<Inline> inlines = new List<Inline>();
txt.Inlines.ToList().ForEach(c => inlines.Add(c)); txt.Inlines.Clear();
_this._txtBlock.Text = null;
foreach (var item in inlines)
{
_this._txtBlock.Inlines.Add(item);
}
_this.UpdateLayout();
}
else
{
_this._txtBlock.Inlines.Clear();
_this._txtBlock.Text = _this.TextSource;
}
}));
}

有些BUG,如果先更新要搜索的字符串,再更新源字符串不会触发事件,但是在实际使用中也是在源字符串里面找要搜索的字符串。搜索字符串里面是加空格分隔的。应该有更好的算法,也可以用正则匹配就用像我代码里面用 XamlReader.Load() ,但是在最先实现中,由于本生自己正则不是好好,所以希望大家提出更好的算法。

SilverLight高亮显示文本的更多相关文章

  1. HTML5基础-Mark标签高亮显示文本

    1.mark标签使用 <mark></mark> 2.mark作用 使用mark标签元素,可以高亮显示文档中的文字以达到醒目的效果. 3.mark使用代码 <!DOCTY ...

  2. Silverlight中文本框添加回车事件后,换行无法清除的解决方法

    在开发Silverlight的项目中,为了更好的用户体验,我们常要给一些控件添加一些快捷键.然而,在Silverlight中当用户回车提交后,光标停留在文本框的第二行怎么也清除不掉,经过一段时间研究, ...

  3. vim高亮显示文本

    行列高亮设置 • 行高亮 " 设置高亮行的颜色,ctermbg设定背景色,ctermfg设定前景色 set cursorline hi CursorLine cterm=NONE cterm ...

  4. Android 高亮显示文本中的关键字

    总结:SpannableString用好,可以各种替换Span来操作各种内容 1.文本关键字高亮关键在于:SpannableString使用 主要就是通过关键字在文本的开始index,结束index来 ...

  5. silverlight 富文本

  6. linux中高亮显示文本的工具 -- bat

    bat 的项目地址 https://github.com/sharkdp/bat bat 是用rust 开发的, 在centos中安装bat需要rust的环境, 我们可以通过安装rust的包管理工具c ...

  7. 【WP8】让TextBox文本支持滑动(Scroll)

    通过修改样式让TextBox支持文本滑动 在Silverlight上,TextBox是有文本滚动的功能的,当TextBox的文本过长时,可以进行拖动的,TextBox使用 VerticalScroll ...

  8. Qt Assistant介绍

    简介 Qt Assistant也就是我们常说的Qt助手,是一款用于呈现在线文档的工具. 简介 一分钟学会使用 Qt参考文档 Qt Assistant详解 命令行选项 工具窗口 文档窗口 工具栏 菜单 ...

  9. 【Qt】Qt Assistant介绍【转】

    简介 Qt Assistant也就是我们常说的Qt助手,是一款用于呈现在线文档的工具. 简介 一分钟学会使用 Qt参考文档 Qt Assistant详解 命令行选项 工具窗口 文档窗口 工具栏 菜单 ...

随机推荐

  1. [CF1111E]Tree

    题目大意:给一棵$n(n\leqslant10^5)$个点的树,有$q(q\leqslant10^5)$次询问,每次询问给出$k,m,r$表示把以下$k$个点分成不超过$m$组,使得在以$r$为根的情 ...

  2. Wifi密码破解实战

    原文链接地址:http://www.freebuf.com/articles/wireless/127261.html https://www.baidu.com/?tn=98012088_4_dg& ...

  3. Mobile Service

    link 试题分析 我们发现$dp(t,s1,s2,s3)$表示在$t$时刻$3$个人的位置.发现时间复杂度为$O(n \times L^3)$.不仅会$T$还会$MLE$,所以需要优化$dp$.我们 ...

  4. Linux应用编程之串口操作20170901

    主要介绍在Linux应用程序下对串口的操作: 1.串口初始化 int InitCom() { int Ret; Ret = SerailComm.OpenCom( ComPortDevPath, 0 ...

  5. echo 不换行

    原文 http://blog.sina.com.cn/s/blog_4da051a6010184uk.html echo -n 不换行输出   $echo -n "123" $ec ...

  6. Creating a Cron Job in K8S

    Creating a Cron Job Cron jobs require a config file. This example cron job config .spec file prints ...

  7. at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142) :json转化“$ref 循环引用”的问题

    原因: entity实体中存在@OneToMany,@ManyToOne注解,在转化json是产生了循环引用 报的错误 解决方法: springmvc @ResponseBody 默认的json转化用 ...

  8. LeetCode-Insertion Sort List[AC源码]

    package com.lw.leet5; /** * @ClassName:Solution * @Description: * Insertion Sort List * Sort a linke ...

  9. linux内核文件系统:proc、tmpfs、devfs、sysfs简要介绍

    linux内核文件系统:proc.tmpfs.devfs.sysfs proc:虚拟文件系统,在linux系统中被挂载与/proc目录下.里面的文件包含了很多系统信息,比如cpu负载. 内存.网络配置 ...

  10. 你知道吗?31种 CSS 选择器的应用

    选择器(selector)是CSS中很重要的概念,所有HTML语言中的标记都是通过不同的CSS选择器进行控制的.用户只需要通过选择器对不同的HTML标签进行控制,并赋予各种样式声明,即可实现各种效果. ...