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

         <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. 【刷题】BZOJ 3926 [Zjoi2015]诸神眷顾的幻想乡

    Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝生日. 粉丝们非常热情,自发组织表演了一系列节目给幽香看. ...

  2. 【刷题】HDU 4966 GGS-DDU

    Problem Description Do you think this is a strange problem name? That is because you don't know its ...

  3. apue.3e 的安装 (基于ubuntu12.0.4)

    本菜刚刚学习UNIX下高级编程,无奈搭建本书编程环境时遇到不少问题.幸好网上有各种大神的解决办法让我最终解决了问题.在这里感谢为LINUX开源操作系统奋斗的大神. 不过话说回来,网上大都是针对UNIX ...

  4. open_basedir restriction in effect的错误及其解决办法

    问题是出现在了PHP.INI上面了   原因是php.ini里设置了     opendir=/var/web/w0895/:/tmp:/usr/lib/php  解答: 其实open_basedir ...

  5. 谷哥的小弟学前端(02)——HTML常用标签(2)

    探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架 ...

  6. [POJ1094] Sorting It All Out

    link 题目大意 给出$m$个不等式关系,问可以从第几个开始确定所有之间的大小关系.若无解请输出是无法确定还是与已知矛盾. 试题分析 这题是真的是坑啊,尽然放在$floyd$传到闭包上面,还用二分, ...

  7. SAS数据步与过程步,数据步语句

    SAS数据步与过程步,数据步语句http://www.biostatistic.net/thread-2045-1-1.html  ---转载---原文作者:biostar(出处: 生物统计家园) 数 ...

  8. bzoj 1879 状压dp

    879: [Sdoi2009]Bill的挑战 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 852  Solved: 435[Submit][Status ...

  9. qq快速登陆

    http://www.cnblogs.com/1996V/p/7481823.html qq快速登陆

  10. TestNG指南

    转载自:http://blog.csdn.net/bigapplestar/article/details/7300137 今天突然收到通知,统一改用TestNG写测试用例,开始查这方面的资料,学习一 ...