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

         <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. a++ 和 ++a 的区别

    a++ 和 ++a 的区别 1)首先说左值和右值的定义:        变量和文字常量都有存储区,并且有相关的类型.区别在于变量是可寻址的(addressable)对于每一个变量都有两个值与其相联:  ...

  2. TCP/IP四层协议模型与ISO七层模型

    TCP/IP四层协议模型与ISO七层模型 在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人的口音,让他 ...

  3. 【Visual Installer】如何读取与写入注册表信息

    引入:using Microsoft.Win32; (1)读取注册表信息 代码: RegistryKey rsg = null; rsg = Registry.LocalMachine.OpenSub ...

  4. (转)Xsl 的Webshell(aspx)版本

    关于使用xsl的webshell以前已经有人发过了,比如aspx的一个webshell如下: <%@ Page Language="C#" Debug="true& ...

  5. acid(数据库事务正确执行的四个基本要素的缩写)

    ACID,指数据库事务正确执行的四个基本要素的缩写.包含:原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Durability).一个支持事务(T ...

  6. SAS8.1安装步骤(附图)

    安装前应当把系统时间更改到一九九几年. 1.在解压后的文件夹里找到 setup .exe 双击 开始安装 2.单击SAS System Setup 3.点击Next 4.选择 complete 并单击 ...

  7. iOS9 HTTP请求失败

    iOS9把所有HTTP请求都改成了HTTPS请求,导致应用加载不出数据. 解决方法:在plist中添加以下新字段 App Transport Security Settings:Dictionary ...

  8. centos7 配置 yum 安装的 jdk

    yum 安装的 java,jdk 路径默认是 /usr/lib/jvm/java-* 我们修改 .bash_profile 文件加上下面几行: export JAVA_HOME=/usr/lib/jv ...

  9. Windows互斥锁demo和分析

    一:windows创建锁接口 创建互斥锁的方法是调用函数CreateMutex HANDLE CreateMutex( LPSECURITY_ATTRIBUTESlpMutexAttributes, ...

  10. 「Python」_init_理解与学习

    Python是面向对象的编程语言,因此我从Class.Instance以及属性(property/attribute)的角度出发解释. _init_根据其英文意思(initialize),用来初始化一 ...